PS

scala.Singleton

scala.Singletonって何?という話

エラーメッセージによると、scala.Singletonfinal traitでありextendsできない。

Path

Scala言語仕様(以下SLS) 3.1より

Paths are not types themselves, but they can be a part of named types ...

ざっくり言うと、valのidentifierが.で繋がったもので、method呼び出しが混ざってはいけない。
objectvalの一種である(SLS 5.4)。

Singleton type

SLS 3.2.1より

A singleton type is of the form p.type, where p is a path pointing to a value expected to conform ( \S6.1) to scala.AnyRef.

Subtype

定義が見当たらない?が、SLS 4.5:

... with respect to subtyping ( \S3.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 type p.type.

Singleton typeのconformance

SLS 3.5.2より

  • A singleton type p.type conforms to the type of the path p.

  • A singleton type p.type conforms to the type scala.Singleton.

前者については

  • A singleton type p.type conforms to the type of the entity the path p 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!

参考文献