使用 sbt dotty 插件:
addSbtPlugin("com.felixmulder" % "sbt-dotty" % "0.1.9")
并运行sbt console
,我尝试了新的联合类型功能:
Starting dotty interpreter...
Welcome to Scala.next (pre-alpha, git-hash: 606e36b) (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112).
Type in expressions to have them evaluated.
Type :help for more information.
scala> case class A(val x: Int, y: Double)
defined class A
scala> case class B(val x: Int, y: String)
defined class B
scala> def getX(o: A | B): Int = o.x
-- [E008] Member Not Found Error: <console> ------------------------------------
8 |def getX(o: A | B): Int = o.x
| ^^^
| value `x` is not a member of (A | B)(o)
为什么这不起作用?我使用联合类型错误吗?这还不行吗?