Advertisement
vadipp

type alias vs value class

Oct 23rd, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.51 KB | None | 0 0
  1. scala> type Location = String
  2. defined type alias Location
  3.  
  4. scala> val loc: Location = "Washington"
  5. loc: Location = Washington
  6.  
  7. //  ----
  8.  
  9. scala> case class Location(location: String) extends AnyVal
  10. defined class Location
  11.  
  12. scala> val loc: Location = "Washington"
  13. <console>:9: error: type mismatch;
  14.  found   : String("Washington")
  15.  required: Location
  16.        val loc: Location = "Washington"
  17.                            ^
  18.  
  19. scala> val loc: Location = Location("Washington")
  20. loc: Location = Location(Washington)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement