delphigenericsdelphi-xe4

Generics and Array of const


type
  TGeneric<T> = class(TBase)
  public
    procedure Swap(Harry : T);
    procedure NotifyAll(AParams : T); 
  end;

i have two question

  1. Is there any way i can typecast a generic variable in any other type like variant etc.

  2. how can i pass generic type in function that accept array of const like here

    Delphi - How can I pass Generic parameter to function that accept Array of const parameter


Solution

  • Is there any way I can typecast a generic variable in any other type like Variant etc.

    As stated in my answer to the question you link, the variant type that works well with generics is TValue. You can make a TValue instance from a generic type using the From method:

    procedure TMyClass.Foo<T>(const Item: T);
    var
      Value: TValue;
    begin
      Value := TValue.From<T>(Item);
    end;
    

    How can I pass generic type in function that accept array of const?

    This is the exact same question that was asked in the question you linked to. The answer is the same as stated there, namely that you cannot pass a generic argument as a variant open array parameter. Again, TValue to the rescue. Instead of using a variant open array parameter, use an open array of TValue.


    The bottom line here is that the variant type that works well with generics, and that is supplied with the Delphi RTL, is TValue.