7. A.
答え: false
scala> List(1).toSet()
<console>:12: warning: Adaptation of argument list by inserting () is deprecated:
signature: GenSetLike.apply(elem: A): Boolean
given arguments: <none>
after adaptation: GenSetLike((): Unit)
List(1).toSet()
^
res4: Boolean = false
なぜ?
47. SCALA 2.12のENUMERATION
拡張しにくい
Color.Value が汚い
object Color extends Enumeration{
val Red,Yellow,Green = Value
}
def show(color:Color.Value):Unit = color match{
case Color.Red => println("赤")
case Color.Yellow => println("黃")
case Color.Green => println("青")
}
50. ENUMを使ったENUMERATIONの例
enum Color{
case Red,Yellow,Green
}
... は以下に展開される
sealed abstract class Color extends scala.Enum
object Color {
private val $values = new scala.runtime.EnumValues[Color]
def enumValue: Map[Int, Color] = $values.fromInt
def enumValueNamed: Map[String, Color] = $values.fromName
def enumValues: Iterable[Color] = $values.values
def $new(tag: Int, name: String): Color = new Color {
def enumTag: Int = tag
override def toString: String = name
$values.register(this)
}
final val Red: Color = $new(0, "Red")
final val Yellow: Color = $new(1, "Yellow")
final val Green: Color = $new(2, "Green")
}
51. ENUM によるENUMERATIONは拡張も容易
enum Color(val name:String){
case Red extends Color("赤")
case Yellow extends Color("黃")
case Green extends Color("青")
}
def show(color:Color):Unit = println(color.name)