scala.Singleton
scala.Singleton
って何?という話
エラーメッセージによると、scala.Singleton
はfinal trait
でありextends
できない。
Path
Scala言語仕様(以下SLS) 3.1より
Paths are not types themselves, but they can be a part of named types ...
ざっくり言うと、val
のidentifierが.
で繋がったもので、method呼び出しが混ざってはいけない。
object
もval
の一種である(SLS 5.4)。
Singleton type
SLS 3.2.1より
A singleton type is of the form
p.type
, wherep
is a path pointing to a value expected to conform (6.1) toscala.AnyRef
.
Subtype
定義が見当たらない?が、SLS 4.5:
... with respect to subtyping (3.5.2).
より、conformance(SLS 3.5.2)のことであると思われる。
Stable type
A stable type is either a singleton type or a type which is declared to be a subtype of
trait scala.Singleton
.
Designator
SLS 6.4より
A designator refers to a named term.
Pathのtyping
SLS 6.4より
The type of a designator is the type T of the entity it refers to, with the following exception: The type of a path
p
which occurs in a context where a stable type is required is the singleton typep.type
.
Singleton typeのconformance
SLS 3.5.2より
A singleton type
p.type
conforms to the type of the pathp
.A singleton type
p.type
conforms to the typescala.Singleton
.
前者については
A singleton type
p.type
conforms to the type of the entity the pathp
refers to.
という意味だと思う。
どうもこれでpathが渡されたかどうか分かるようだ。
Pathの型およびsingleton typeは、scala.Singleton
にconformする。
例
def single(x: scala.Singleton) = () object a { val b: String = "hello" def fb: b.type = b def fd: String = "bye" } def fa: a.type = a single(a) // ok, path single(a.b) // ok, path single(a.fb) // ok, singleton single(a.fd) // error! single(identity(a).b) // error! single(fa.b) // error!