Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala case class equals (==) not working as expected

I must be missing something silly here. I have this:

case class Color(val rgb:Int) {
   private val c = rgb - 0xff000000
   val r = (c & 0xff0000) >> 16
   val g = (c & 0x00ff00) >> 8
   val b = (c & 0x0000ff)
}

case object Red extends Color(0xffff0000)
case object Green extends Color(0xff00ff00)
case object Blue extends Color(0xff0000ff)

Then I expect this to print true:

val c = Color(0xff00ff00)
println(c == Green)

Why doesn't it??

like image 296
Germán Avatar asked Aug 23 '11 19:08

Germán


1 Answers

Case classes (or objects) inheriting from case classes is a bad practice, and is illegal as of Scala 2.9.1. Use object instead of case object to define Red, Green and Blue.

like image 129
4e6 Avatar answered Nov 05 '22 16:11

4e6