3
在斯卡拉,爲什麼設置方法類型參數的較低類型邊界不強制方法爭論的「是超類型」限制?斯卡拉更低類型綁定'是方法參數的子類型'限制
object TypeBounds extends App {
class MotorVehicle
class Truck extends MotorVehicle
class Car extends MotorVehicle
class Saloon extends Car
class HatchBackSaloon extends Saloon
def lowerTypeBound[C >: Car](c: C): C = c
def upperTypeBound[C <: Car](c: C): C = c
// Works. HatchBackSaloon is a sub class of Car
println(upperTypeBound(new HatchBackSaloon()))
// as expected doesn't compile. Truck is not a subclass of Car
println(upperTypeBound(new Truck()))
// Compiles and runs, but why ? HatchBackSaloon is not a super class of Car.
println(lowerTypeBound(new HatchBackSaloon()))
}
我想這種回答我的問題[如何Java編譯器爲下界通配符進行類型擦除?](http://stackoverflow.com/questions/25480172/how-does-the-java-compiler-perform型擦除換下界-通配符) – tdmadeeasy