Matching sub-classes of case classes in Scala -


why fail compile (or work?):

  case class a(x: int)   class b extends a(5)    (new b) match {     case a(_) => println("found a")     case _ => println("something else happened?")   } 

the compiler error is:

constructor cannot instantiated expected type;  found   : blevins.example.app.a  required: blevins.example.app.b 

note compiles , runs expected:

  (new b) match {     case a: => println("found a")     case _ => println("something else happened?")   } 

addendum

just reference, compiles , runs fine:

  class a(val x: int)   object {     def unapply(a: a) = some(a.x)   }   class b extends a(5)    (new b) match {     case a(i) => println("found a")     case _ => println("something else happened?")   } 

this works, @ least in 2.8:

scala>   case class a(x: int)                            defined class  scala>   class b extends a(5)                            defined class b  scala>   (new b: a) match {                                   |     case a(_) => println("found a")                    |     case _ => println("something else happened?")      |   }                                               found 

i haven't found pointer particular bug causes original problem, ignore warnings case class inheritance @ own peril.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -