独角兽企业重金招聘Python工程师标准>>>
什么是幽灵类型?
一个幽灵类型是在运行时表现为没有副作用的抽象类型。 因为他们在运行时没有副作用,所以一旦它显示明确类型之后,会被编译器从结果集中擦除。
如果一个函数接受一个幽灵类型,那么他的返回值将不会受他的参数影响。
定义一个幽灵类型
幽灵类型是通过一个object
继承scala.Phantom
来定义的,这个对象表示幽灵类型的一个领域,是完全区别开scala.Any
或者其他的幽灵类型的领域。
package scalatrait Phantom { // only an `object` can extend this traitprotected final type Any // not a subtype of scala.Anyprotected final type Nothing // subtype of every subtype of this.Anyprotected final def assume: this.Nothig}object MyPhantoms extends Phantom
如果觉得《dotty编译器语法特性之一幽灵类型》对你有帮助,请点赞、收藏,并留下你的观点哦!