scala - scala.Singleton 是纯编译器小说吗?

标签 scala types singleton language-design specifications

Scala Language Specification在 §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.



我找不到 scala.Singleton既不在源代码中,也不在 ScalaDoc 中,也不在二进制 jar 文件中。

尝试 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生活,它的​​目的是什么?

最佳答案

终于在这方面找到了一些东西:

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 } 

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

关于scala - scala.Singleton 是纯编译器小说吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118038/

相关文章:

scala - 像普通的 Seq 一样对待 Spark RDD

java - 在磁盘上写入文件的进程每秒的请求数较低

c++ - 运算符<<在单例中的重新定义

c++ - 在C++中传递函数类型的参数是什么意思?

python - 这个变量 `instantiated` 的范围是什么

java - 在 Java 中实现单例模式的有效方法是什么?

scala - 我可以在 Scala 中定义和使用类和对象之外的函数吗?

scala - Json Coast to Coast Play框架: Serializing Joda DateTime

c++ - 获取 C++ 中表达式的类型,包括引用

mysql - MySQL 中数据类型的动态大小