Is scala.Singleton pure compiler fiction?

孤街浪徒 提交于 2019-12-01 01:50:07

问题


The Scala Language Specification says under §3.2.1:

A stable type is either a singleton type or a type which is declared to be a subtype of trait scala.Singleton.

I couldn't find scala.Singleton neither in the sources, in ScalaDoc nor in the binary jar file.

Trying on the REPL results in:

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
                 ^

Where does scala.Singelton live and what's its purpose?


回答1:


Finally found something on this:

The type Singleton is essentially an encoding trick for existentials with values. I.e.

T forSome { val x: T } 

is turned into

[x.type := X] T forSome { type X <:T with Singleton } 

Source: http://scala-programming-language.1934581.n4.nabble.com/scala-Singleton-td1940630.html



来源:https://stackoverflow.com/questions/6118038/is-scala-singleton-pure-compiler-fiction

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