Last active
August 29, 2015 14:07
-
-
Save d6y/359b88789b8a9b67cb12 to your computer and use it in GitHub Desktop.
Value Types via Syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We don't want to do this: | |
case class Contact(person: String, address: String) | |
// We prefer: | |
class Name(val value: String) extends AnyVal | |
class Email(val value: String) extends AnyVal | |
case class Contact(person: Name, address: Email) | |
// Can we reduce the friction on the value type creation? | |
// Perhaps use the fact the val and class are keyword: | |
val class Name = String // Read as "value class Name is a String" | |
val class Email = String | |
case class Contact(person: Name, address: Email) | |
// This could be a desuggaring to the extends AnyVal form. | |
// Extensions: | |
val case class Name = String | |
// and | |
val class Email = String { | |
def parts = value splitAt "@" | |
} | |
// and | |
val class Name = String extends Thing | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem: values classes cannot be inside other classes. Using a val at the top-level like that might be a problem in that your intuition on where you can use "val class" would be wrong.