scala – NotNull tr??ait在2.8中是如何工作的,有没有人实际使用
发布时间:2020-12-16 09:13:28 所属栏目:安全 来源:网络整理
导读:trait NotNull {} 我一直在试图看看这个特质如何保证一些东西不为空,我无法理解: def main(args: Array[String]) { val i = List(1,2) foo(i) //(*)}def foo(a: Any) = println(a.hashCode)def foo(@NotNull a: Any) = println(a.hashCode) //compile error
trait NotNull {} 我一直在试图看看这个特质如何保证一些东西不为空,我无法理解: def main(args: Array[String]) { val i = List(1,2) foo(i) //(*) } def foo(a: Any) = println(a.hashCode) def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*) 和: val i = new Object with NotNull //compile-error illegal inheritance 显然有一些特殊的编译器处理过程,因为这样编译: trait MyTrait {} def main(args: Array[String]) { val i: MyTrait = null println(i) } 而这不是: def main(args: Array[String]) { val i: NotNull = null //compile error: found Null(null) required NotNull println(i) } 编辑:没有什么可以在Scala的编程中找到 解决方法
尝试和错误:
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 这只适用于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 和Scala语言参考:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Angular 进阶:从源码理解@Input绑定是如何被编译和实现的
- Apache使用 .htaccess 来实现强制https访问的方法
- 使用bash脚本解析字符串以获取最后一项
- 如何用scala编写eclipse rcp应用程序?
- 在角度2(RC5)中使用多个自定义模块
- Jupyter Scala Kernel Missing
- angular-cli – Angular cli更新后获取错误
- scala – Spark配置:SPARK_MEM与SPARK_WORKER_MEMORY
- scala – 在intellij的sbt项目中使用spark
- twitter-bootstrap-3 – 使用ASP.NET MVC 5和Bootstrap 3默