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:
pattern matching:
case _: foo.type
checks matched object samefoo
usingeq
,case foo
checks it's equalfoo
usingequals
.it's needed write down type of
object
(as type parameter, argument, etc.)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 withoutthis.type
becausemethod1
returna
.
Comments
Post a Comment