Does Delphi support lower / upper type bounds for its generics, e.g. such as Scala does?
I did not find anything about it in the Embarcadero docs:
Moreover, there is an implicit hint against type bounds at "Constraints in Generics":
Constraint items include:
- Zero, one, or multiple interface types
- Zero or one class type
- The reserved word "constructor", "class", or "record"
You can specify both "constructor" and "class" for a constraint. However, "record" cannot be combined with other reserved words. Multiple constraints act as an additive union ("AND" logic).
Example:
Let's look at the behavior in the following Scala code, that demonstrates the usage of an upper bound type restriction. I found that example 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
}
}
Is there any way to achieve such a behavior in Delphi?