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

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -