How does the NotNull trait work in 2.8 and does anyone actually use it?

自古美人都是妖i 提交于 2019-11-29 03:48:46

Try and error:

scala> class A extends NotNull
defined class A

scala> val a : A = null
<console>:5: error: type mismatch;
 found   : Null(null)
 required: A
       val a : A = null
                   ^

scala> class B
defined class B

scala> val b : B = null
b: B = null

This works only with Scala 2.7.5:

scala> new Object with NotNull
res1: java.lang.Object with NotNull = $anon$1@39859

scala> val i = new Object with NotNull
i: java.lang.Object with NotNull = $anon$1@d39c9f

And the Scala Language Reference:

If that member has a type which conforms to scala.NotNull, the member’s valuemust be initialized to a value different from null, otherwise a scala.UnitializedError is thrown.

For every class type T such that T <: scala.AnyRef and not T <: scala.NotNull one has scala.Null <: T.

NotNull is not yet finished. The intention is to evolve this into a usable way to check for non-nullness but it's not yet there. For the moment I would not use it. I have no concrete predictions when it will be done, only that it won't arrive for 2.8.0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!