为什么实例变量没有使用可选的?

Mic*_*ael 8 java scala optional

我已经阅读了很多有关Optional应该使用的案例的信息。

我读过的很多页面都说Optional不应将其用于私有实例变量,而应由getter返回。

我本以为将私有实例变量作为可选变量仍然有用。如果有人查看我的代码,他们可以看到值可以为空,而不必检查文档以查看是否可以返回null。

在Scala中,从不使用null,只有在与Java互操作的情况下才使用null。如果值可以为null,建议始终使用可选值。这种方法对我来说更有意义。

这是提及它的页面:

https://blog.joda.org/2015/08/java-se-8-optional-pragmatic-approach.html

这是示例代码。

private final String addressLine;  // never null
private final String city;         // never null
private final String postcode;     // optional, thus may be null

// normal getters
public String getAddressLine() { return addressLine; }
public String getCity() { return city; }

// special getter for optional field
public Optional<String> getPostcode() {
  return Optional.ofNullable(postcode);
}
Run Code Online (Sandbox Code Playgroud)

我可以看到的唯一好处是,如果您要序列化对象,则现在可以实现,因为它不会在变量中存储可选内容。

缺点是,在检查getter的返回类型之前,您不知道邮政编码可以为null。如果您不熟悉该代码,则可能会错过此添加操作,从而扩展了类,从而导致空指针异常。

这是一个有关Scala的问题Option

何时使用选件

为什么Java和Scala在使用可选方式方面有区别?

Krz*_*sik 9

Not all Java developers even agree with the approach you described. Please check this post by the creator of Lombok.

I guess that the reason for a different approach of using Optional in Java is that Java's community lived without it up till Java 8, so most people were used to null. In one hand a lot of new APIs (like findAny from Stream) return Optional, but still a lot of standard library methods just return null, so you always have to remember either to wrap your function calls with Optional.ofNullable or check if the value is not null.

Optional was added to Java 8 but it is discouraged to use it as class fields because Optional is not implementing Serializable (and Java's serialization is used as the default serialization engine by many frameworks or systems like Akka, Spark, Kafka, etc.).

On the other hand Option is very tightly bound with the Scala standard library. As far as I know, no Scala's standard library APIs return null, but Option, and it is discouraged to use null at all in your Scala code. You can even configure your project that it will fail to compile if null is used.

Option is also Serializable and its normal practice to use is as class fields for values that can be empty.

If you want to use a similar approach in your Java code please check Option from Vavr. It's serializable, so it can be safely used as fields and it also has two subclasses None and Some (similarly as Scala's Option), so it can be used in Vavr's pattern matching:

Match(option).of(
    Case($Some($()), "defined"),
    Case($None(), "empty")
);
Run Code Online (Sandbox Code Playgroud)


Gal*_*aor 5

在Scala中,Option已紧密集成到该语言的API中。

代表可选值。Option的实例是scala的实例。Some或对象None。使用scala.Option实例的最惯用方式是将其视为集合或monad,并使用map,flatMap,filter或foreach。

从上面的引用中可以看到,这里没有null解释,因为它应该用作mondad或collection。

在Java中,通过将Optional用作以下命令,可以使我们免于NullPointerException的情况:

可以包含或不包含非null值的容器对象。如果存在值,则isPresent()将返回true,而get()将返回该值。

通过使用可以非常清楚地向用户显示变量是否可以为null的编程语言是Kotlin 。安全调用,并向您显示编译错误:

var a: String = "abc"
a = null // compilation error

var b: String? = "abc"
b = null // ok
print(b)
Run Code Online (Sandbox Code Playgroud)