If you consider a type as a set of values, the singleton value type x is a type that contains only that value ( {x} ). Examples of using:
Pattern matching: case _: Foo.type checks to see if the matching object matches Foo using eq , while case Foo checks to see if it matches Foo using equals .
It was necessary to write the type object (as a type parameter, argument, etc.):
object A def method(): A.type = A
For mutable objects, to guarantee the return value of a method is an object (useful for a chain of methods, an example here ):
class A { def method1: this.type = { ...; this } } class B extends A { def method2: this.type = { ...; this } }
Now you can call new B.method1.method2 , which you could not have without this.type , because method1 will return. A
source share