Tuesday, August 25, 2009

Equals

One welcome change in Scala is the change of semantics of the == operator. In Scala it is the same as doing a .equals comparison in Java. If you want identity comparison you can use the eq method (and ne for the not equal comparator).

Examples:
  1. scala>caseclass Name(first:String, last:String)
  2. defined class Name
  3. scala>val jesse = Name("Jesse","Eichar")
  4. jesse: Name = Name(Jesse,Eichar)
  5. scala>val jesse2 = Name("Jesse","Eichar")
  6. jesse2: Name = Name(Jesse,Eichar)
  7. scala>val jody = Name("Jody","Garnett")
  8. jody: Name = Name(Jody,Garnett)
  9. scala>val jJames = Name("Jesse","James")
  10. jJames: Name = Name(Jesse,James)
  11. scala> jesse == jesse2
  12. res0: Boolean = true
  13. scala> jesse == jody
  14. res1: Boolean = false
  15. scala> jesse ne jesse2
  16. res2: Boolean = true
  17. scala> jesse eq jesse2
  18. res3: Boolean = false

No comments:

Post a Comment