Scala case class constructor with WrappedArray argument
I have the following scenario:
sealed abstract class Type(val inUse: Boolean)
case class IntTy(override val inUse: Boolean) extends Type(inUse)
case class TupleTy(override val inUse: Boolean, elems: Type*) extends Type(inUse) {
def this(elems: Type*) = this(false, elems:_*)
}
In Scala 2.8.0 this works just fine and I can create a new TupleTy instance with:
TupleTy(IntTy(false))
However, I've just updated to 开发者_运维知识库Scala 2.9.1 final and it no longer works. I now get the following error:
error: type mismatch;
found : IntTy
required: Boolean
TupleTy(IntTy(false))
^
Is this a bug or am I missing somehing?
I'm not sure that it works in 2.8.0.
You have defined an additional constructor, but not an additional factory method.
new TupleTy(IntTy(false)) // works as expected
EDIT
Here is a possible workaround
case class TupleTy(override val inUse: Boolean = false)(elems: Type*) extends Type(inUse)
Now you can do ugly things like this, but you shouldn't. No really, you shouldn't.
TupleTy()(IntTy(false))
creating your TupleTy with "new" (like with a regular class) works:
scala> new TupleTy(IntTy(false))
res3: TupleTy = TupleTy(false,WrappedArray(IntTy(false)))
additional constructors for case classes need to be called with "new", because (unlike the default constructor) the don't get "translated" to an apply method on the companion object. Note that the "unapply" does not get generated either, so pattern matching on your TupleTy may not work as intended.
Here's some background discussion on scala-lang.org as to why the scala compiler has not been augmented to handle multiple case class constructors.
EDIT you can create the additional "apply" yourself, if you want:
object TupleTy {
def apply(elems: Type*) = new TupleTy(false, elems:_*)
}
with that, you can do:
scala> TupleTy(IntTy(false))
res4: TupleTy = TupleTy(false,WrappedArray(IntTy(false)))
精彩评论