gpt4 book ai didi

java - scala 和 java 枚举之间的区别

转载 作者:搜寻专家 更新时间:2023-10-31 08:26:11 25 4
gpt4 key购买 nike

我读了an answer on SO where someone said that scala enumerations are useless , 如果你真的需要的话,你应该只使用 java 枚举。

虽然我以前使用过 java 枚举,但我不能说我完全理解它们,因为我在日常工作中不使用 java 编写代码。

谁能解释一下 scala 和 java 枚举之间的区别,以及 scala 枚举的缺点到底在哪里?

最佳答案

Scala 枚举的主要优点是语法的规律性。

如果您想向枚举的元素添加行为,只需扩展其 Val 类即可。

Odersky 喜欢它们作为命名 int 值常量的最简单用例。他刚刚在邮件列表中首次 promise ,改进的支持即将到来。

但是我recently used它们用于替换字符串列表,因为值集有点设置,比一组用于查找的字符串更好。

代替

val stuff = List("foo", "bar", "baz")

object stuff extends Enumeration { val foo, bar, baz = Value }

scala> object stuff extends Enumeration { val foo, bar, baz = Value }
defined object stuff

scala> val junk = new Enumeration { val foo, bar, baz = Value }
junk: Enumeration{val foo: this.Value; val bar: this.Value; val baz: this.Value} = 1

scala> stuff.values contains junk.foo
<console>:10: error: type mismatch;
found : junk.Value
required: stuff.Value
stuff.values contains junk.foo
^

行为:

scala> trait Alias { def alias: String }
defined trait Alias

scala> object aliased extends Enumeration {
| class Aliased extends Val with Alias {
| def alias = toString.permutations.drop(1).next }
| val foo, bar, baz = new Aliased }
defined object aliased

scala> abstract class X { type D <: Enumeration
| def f(x: D#Value) = x match { case a: Alias => a.alias
| case _ => x.toString } }
defined class X

scala> class Y extends X { type D = aliased.type }
defined class Y

scala> new Y().f(aliased.bar)
res1: String = bra

scala> new Y().f(stuff.foo)
<console>:13: error: type mismatch;
found : stuff.Value
required: aliased.Value
new Y().f(stuff.foo)
^

scala> new X { type D = junk.type }.f(junk.foo)
warning: there was one feature warning; re-run with -feature for details
res4: String = foo

ValueSet 是位设置:

scala> stuff.values contains aliased.bar
<console>:11: error: type mismatch;
found : aliased.Aliased
required: stuff.Value
stuff.values contains aliased.bar
^

scala> stuff.foo + aliased.bar
<console>:11: error: type mismatch;
found : aliased.Aliased
required: stuff.Value
stuff.foo + aliased.bar
^

scala> stuff.foo + stuff.bar
res8: stuff.ValueSet = stuff.ValueSet(foo, bar)

其他似乎在今天的 Scala 中有效的东西:

scala> def f[E <: Enumeration](e: E)(v: e.Value) = e.ValueSet.empty + v
f: [E <: Enumeration](e: E)(v: e.Value)e.ValueSet

scala> f(stuff)(stuff.foo)
res14: stuff.ValueSet = stuff.ValueSet(foo)

scala> def g[E <: Enumeration](e: E)(a: Any) = a match { case _: e.Value => true case _ => false }
g: [E <: Enumeration](e: E)(a: Any)Boolean

scala> g(stuff)(stuff.foo)
res15: Boolean = true

scala> g(stuff)(junk.foo) // checking outer pointers
warning: there was one feature warning; re-run with -feature for details
res16: Boolean = false

scala> g(stuff)(aliased.foo)
res17: Boolean = false

看起来 Scala 是 not entirely friendly to Java enums :

scala> Thread.State.NEW.ordinal
[snip]
scala.reflect.internal.FatalError:
Unknown type: <notype>(NEW), <notype> [class scala.reflect.internal.Types$UniqueConstantType, class scala.reflect.internal.Types$NoType$] TypeRef? false
while compiling: <console>
during phase: icode
library version: version 2.11.2
compiler version: version 2.11.2
reconstructed args:

last tree to typer: Apply(method ordinal)
tree position: line 8 of <console>
tree tpe: Int
symbol: final method ordinal in class Enum
symbol definition: final def ordinal(): Int (a MethodSymbol)
symbol package: java.lang
symbol owners: method ordinal -> class Enum
call site: constructor $read$$iw$$iw in package $line4

关于java - scala 和 java 枚举之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25571389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com