Java 8的可选、Scala的选项和Haskell的选项之间的关键区别是什么?

时间:2022-11-14 17:03:56

I've read a few posts on Java 8's upcoming Optional type, and I'm trying to understand why people keep suggesting it's not as powerful as Scala's Option. As far as I can tell it has:

我读过一些关于Java 8即将推出的可选类型的文章,我试图理解为什么人们总是说它不如Scala的选项强大。据我所知:

  • Higher order functions like map and filter using Java 8 lambdas.
  • 像使用Java 8 lambdas的map和filter这样的高级函数。
  • Monadic flatMap
  • 一元flatMap
  • Short circuiting through getOrElse type functions.
  • 通过getOrElse类型函数的短路。

What am I missing?

我缺少什么?

3 个解决方案

#1


7  

Some possibilities come to mind (OTOH, I haven't seen people actually saying that, so they might mean something else):

我想到了一些可能性(OTOH,我没见过人们真的这么说,所以他们可能还有别的意思):

  1. No pattern matching.

    没有模式匹配。

  2. No equivalent to Scala's fold or Haskell's fromMaybe: you have to do optional.map(...).orElseGet(...) instead.

    与Scala的折叠和Haskell的fromMaybe不同:你必须选择。map(…). orelseget(…)。

  3. No monadic syntax.

    没有一元语法。

I also wouldn't call any of these "less powerful" myself, since you can express everything you can with the corresponding Scala/Haskell types; these are all conciseness/usability concerns.

我也不认为这些功能“不太强大”,因为您可以使用相应的Scala/Haskell类型来表达所有您能表达的内容;这些都是简洁性/可用性方面的问题。

#2


8  

Optional and Maybe are effectively in correspondence. Scala has None and Some[A] as subclassing Option[A] which might be more directly comparable to Java since Java could have done the same.

可选的,可能是有效的通信。Scala没有和一些[A]作为子类选项[A],这可能更直接地与Java进行比较,因为Java也可以这样做。

Most other differences either have to do with the ease of handling Maybe/Option in Haskell/Scala which won't translate since Java is less expressive as a language or the consistency of use of Maybe/Option in Haskell/Scala where many of the guarantees and conveniences afforded by the type only kick in once most libraries have agreed to use optional types instead of nulls or exceptions.

大多数其他的差别和易于处理也许Haskell /选项/ Scala不会翻译自Java不太表达作为一种语言或使用可能/选项的一致性在Haskell / Scala提供的担保和便利的许多类型只能启动一次大多数图书馆已经同意使用可选的类型而不是null或异常。

#3


0  

For most purposes, they are equivalent; the main difference is that the Scala one is well-integrated into Scala, while the Java one is well-integrated into Java.

对于大多数目的来说,它们是等价的;主要的不同之处在于Scala集成得很好,而Java集成得很好。

The biggest difference in my mind is that Java's is a value-based class. That's something new to the JVM. At the moment there's no real difference between value-based and regular classes, but the distinction paves the way for JVM runtimes to eliminate Java object overhead. In other words, a future JVM could rewrite Optional code as a directive for how to handle nulls, rather than allocating memory for Optional objects.

我认为最大的不同在于Java是一个基于值的类。这是JVM的新特性。目前,基于值的类和常规类之间没有真正的区别,但是这种区别为JVM运行时消除Java对象开销铺平了道路。换句话说,未来的JVM可以将可选代码重写为如何处理null的指令,而不是为可选对象分配内存。

Scala does something similar with value classes, though it's done by unboxing types in the compiler rather than in the JVM, and its usage is limited. (Option isn't a value class.)

Scala对值类做了类似的事情,尽管它是通过在编译器中而不是JVM中解压缩类型来完成的,并且它的使用是有限的。(选项不是一个值类。)

#1


7  

Some possibilities come to mind (OTOH, I haven't seen people actually saying that, so they might mean something else):

我想到了一些可能性(OTOH,我没见过人们真的这么说,所以他们可能还有别的意思):

  1. No pattern matching.

    没有模式匹配。

  2. No equivalent to Scala's fold or Haskell's fromMaybe: you have to do optional.map(...).orElseGet(...) instead.

    与Scala的折叠和Haskell的fromMaybe不同:你必须选择。map(…). orelseget(…)。

  3. No monadic syntax.

    没有一元语法。

I also wouldn't call any of these "less powerful" myself, since you can express everything you can with the corresponding Scala/Haskell types; these are all conciseness/usability concerns.

我也不认为这些功能“不太强大”,因为您可以使用相应的Scala/Haskell类型来表达所有您能表达的内容;这些都是简洁性/可用性方面的问题。

#2


8  

Optional and Maybe are effectively in correspondence. Scala has None and Some[A] as subclassing Option[A] which might be more directly comparable to Java since Java could have done the same.

可选的,可能是有效的通信。Scala没有和一些[A]作为子类选项[A],这可能更直接地与Java进行比较,因为Java也可以这样做。

Most other differences either have to do with the ease of handling Maybe/Option in Haskell/Scala which won't translate since Java is less expressive as a language or the consistency of use of Maybe/Option in Haskell/Scala where many of the guarantees and conveniences afforded by the type only kick in once most libraries have agreed to use optional types instead of nulls or exceptions.

大多数其他的差别和易于处理也许Haskell /选项/ Scala不会翻译自Java不太表达作为一种语言或使用可能/选项的一致性在Haskell / Scala提供的担保和便利的许多类型只能启动一次大多数图书馆已经同意使用可选的类型而不是null或异常。

#3


0  

For most purposes, they are equivalent; the main difference is that the Scala one is well-integrated into Scala, while the Java one is well-integrated into Java.

对于大多数目的来说,它们是等价的;主要的不同之处在于Scala集成得很好,而Java集成得很好。

The biggest difference in my mind is that Java's is a value-based class. That's something new to the JVM. At the moment there's no real difference between value-based and regular classes, but the distinction paves the way for JVM runtimes to eliminate Java object overhead. In other words, a future JVM could rewrite Optional code as a directive for how to handle nulls, rather than allocating memory for Optional objects.

我认为最大的不同在于Java是一个基于值的类。这是JVM的新特性。目前,基于值的类和常规类之间没有真正的区别,但是这种区别为JVM运行时消除Java对象开销铺平了道路。换句话说,未来的JVM可以将可选代码重写为如何处理null的指令,而不是为可选对象分配内存。

Scala does something similar with value classes, though it's done by unboxing types in the compiler rather than in the JVM, and its usage is limited. (Option isn't a value class.)

Scala对值类做了类似的事情,尽管它是通过在编译器中而不是JVM中解压缩类型来完成的,并且它的使用是有限的。(选项不是一个值类。)