Skip to content

Instantly share code, notes, and snippets.

@d6y
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d6y/359b88789b8a9b67cb12 to your computer and use it in GitHub Desktop.
Save d6y/359b88789b8a9b67cb12 to your computer and use it in GitHub Desktop.
Value Types via Syntax
// 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
@d6y
Copy link
Author

d6y commented Oct 18, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment