Does Delphi Generics support Lower / Upper Type Bounds?
Asked Answered
R

1

10

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?

Root answered 24/9, 2018 at 12:22 Comment(2)
I think you want contravariance.Fletafletch
@Fletafletch Delphi does not support co- or contravariance.Lukewarm
L
13

In Delphi this example would look like following (stripped irrelevant code):

type
  TAnimal = class(TObject);

  TDog = class(TAnimal);

  TPuppy = class(TDog);

  TAnimalCarer = class
    procedure Display<T: TDog>(dog: T);
  end;

var
  animal: TAnimal;
  dog: TDog;
  puppy: TPuppy;
  animalCarer: TAnimalCarer;
begin
//  animalCarer.Display(animal); // [dcc32 Error] E2010 Incompatible types: 'T' and 'TAnimal'
  animalCarer.Display(dog);
  animalCarer.Display(puppy);
end.

It is not possible to specify a lower bound as shown in the article you linked to because Delphi does not support that. It also does not support any type variance.

Edit: FWIW in this case the Display method would not even have to be generic and the dog parameter could just be of type TDog as you then can pass any subtype. Because of the limited capabilities of generics in Delphi the Display method would not benefit of being generic.

Lukewarm answered 24/9, 2018 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.