2 ms·
Scala has value types. case class MyNewType(x: String) extends AnyVal What do you want to do that that isn't sufficient for?
by yummyfajitas 13y ago
Scala has value types.
case class MyNewType(x: String) extends AnyVal
What do you want to do that that isn't sufficient for?
- kasey_junk 13y agoAnyVal's would probably be sufficient except for the following: - Instantiation of the MyNewType happens all the time and is not obvious from inspection of the code (generics, use as another type if we implement a trait, etc). - If we want to call any of the methods on the wrapped type we have to call directly to the wrapped type violating Demeter. - Verbosity.
- yummyfajitas 13y agoIf we want to call any of the methods on the wrapped type we have to call directly to the wrapped type violating Demeter. You can also use tagged types: http://eed3si9n.com/learning-scalaz/Tagged+type.html http://eed3si9n.com/learning-scalaz/Tagged+type.html But most of the methods will untag the type. I.e., ("foo" @@ ValidJsonString).substring(3,7) will be a String, not a String @@ ValidJsonString.