scala - What is a Singleton Type exactly? -


what singleton type? applications, implications ?

examples more welcome , layman terms more welcome !

if think of type set of values, singleton type of value x type contains value ({x}). usage examples:

  1. pattern matching: case _: foo.type checks matched object same foo using eq, case foo checks it's equal foo using equals.

  2. it's needed write down type of object (as type parameter, argument, etc.)

  3. for mutable objects guarantee return value of method object (useful method chaining, example from here):

    class { def method1: this.type = { ...; } } class b extends { def method2: this.type = { ...; } } 

    you can call new b.method1.method2, couldn't without this.type because method1 return a.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -