Delphi是否支持其泛型的lower / upper type bounds,例如Scala可以支持的那样?
我在Embarcadero文档中没有找到相关内容:
此外,在“泛型约束”一章中有一个隐含的提示反对类型边界:
约束项包括:
- 零个、一个或多个接口类型
- 零个或一个类类型
- 保留字"constructor","class"或"record"
您可以为约束指定"constructor"和"class"。 但是,"record"不能与其他保留字组合。 多个约束作为加法并集(“AND”逻辑)。
示例:
让我们来看下面这段Scala代码的行为,它演示了上限类型限制的用法。我在网上找到了这个例子on the net:
class Animal
class Dog extends Animal
class Puppy extends Dog
class AnimalCarer{
def display [T <: Dog](t: T){ // Upper bound to 'Dog'
println(t)
}
}
object ScalaUpperBoundsTest {
def main(args: Array[String]) {
val animal = new Animal
val dog = new Dog
val puppy = new Puppy
val animalCarer = new AnimalCarer
//animalCarer.display(animal) // would cause a compilation error, because the highest possible type is 'Dog'.
animalCarer.display(dog) // ok
animalCarer.display(puppy) // ok
}
}
有没有办法在Delphi中实现这样的行为?