Tutorial by Examples

type Meter = Double This simple approach has serious drawbacks for unit handling as every other type that is a Double will be compatible with it: type Second = Double var length: Meter = 3 val duration: Second = 1 length = duration length = 0d All of the above compiles, so in this case un...
case class Meter(meters: Double) extends AnyVal case class Gram(grams: Double) extends AnyVal Value classes provide a type-safe way to encode units, even if they require a bit more characters to use them: var length = Meter(3) var weight = Gram(4) //length = weight //type mismatch; found : Gr...

Page 1 of 1