【问题标题】:Differences between scala and java enumerationsscala和java枚举的区别
【发布时间】:2014-08-29 15:24:53
【问题描述】:

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

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

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

【问题讨论】:

标签: java scala enumeration


【解决方案1】:

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

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

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

但是我recentlyused他们来替换一个字符串列表,因为这组值有点设置,比一组字符串更好查找。

代替

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-27
    • 2010-10-31
    • 2014-02-27
    • 2016-01-02
    • 1970-01-01
    • 2016-11-26
    • 2010-12-06
    相关资源
    最近更新 更多