Open
Description
Reproduction steps
Scala version: 2.13.11
import castingUtil.safeCasting
class Squirrel[Nut](val nuts :Array[Nut]) {
def copy[N](nuts :Array[N] = nuts) :Squirrel[N] = //compiles, but should not
new Squirrel(nuts)
}
class Hamster[Seed](val seeds :Array[Seed]) { //does not compile, but should
def copy[S](seeds :Array[S] = seeds.cast[Array[Seed], Array[S]]) :Hamster[S] =
new Hamster(seeds)
}
object castingUtil {
implicit class safeCasting[T](private val self :T) {
def cast[From >: T, To] :To = self.asInstanceOf[To]
}
}
Problem
type arguments [Array[Seed],Array[S]] do not conform to method cast's type parameter bounds [From >: Array[S],To]
def copy[S >: Seed](seeds :Array[S] = seeds.cast[Array[Seed], Array[S]]) :Hamster[S] =
I can't even put it into words, but apparently nuts
/seeds
have their type parameters mistyped in this context to the method type parameter, rather than owning class's type parameter.
Also, removing val
keywords and leaving the properties as pure fields still work, that is are legal for use as default parameters.
I don't report it, because it probably is a feature.