scala.Singleton是纯编译器小说吗? [英] Is scala.Singleton pure compiler fiction?
问题描述
Scala语言规范在第3.2.1节中说:
The Scala Language Specification says under §3.2.1:
稳定类型是单例 类型或声明为 性状scala.Singleton的子类型.
A stable type is either a singleton type or a type which is declared to be a subtype of trait scala.Singleton.
在源代码,ScalaDoc和二进制jar文件中都找不到scala.Singleton
.
I couldn't find scala.Singleton
neither in the sources, in ScalaDoc nor in the binary jar file.
尝试REPL会导致:
scala> class Foo extends Singleton
<console>:9: error: illegal inheritance from final trait Singleton
class Foo extends Singleton
^
<console>:9: error: illegal inheritance; superclass Any
is not a subclass of the superclass Object
of the mixin trait ScalaObject
class Foo extends Singleton
^
scala.Singelton
住在哪里,目的是什么?
Where does scala.Singelton
live and what's its purpose?
推荐答案
最终在此找到了一些东西:
Finally found something on this:
Singleton类型本质上是一个 存在的编码技巧 价值观. IE.
The type Singleton is essentially an encoding trick for existentials with values. I.e.
T forSome { val x: T }
变成了
[x.type := X] T forSome { type X <:T with Singleton }
来源: http://scala-programming-language.1934581.n4 .nabble.com/scala-Singleton-td1940630.html
这篇关于scala.Singleton是纯编译器小说吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!