Overloads and Type Compatibility in Generics
Go Up to Generics Index
Generic methods can participate in overloading alongside non-generic methods by using the 'overload' directive. If overload selection between a generic method and a non-generic method would otherwise be ambiguous, the compiler selects the non-generic method.
type TFoo = class procedure Proc<T>(A: T); overload; procedure Proc(A: String); overload; procedure Test; end; procedure TFoo.Test; begin Proc('Hello'); // calls Proc(A: String); Proc<String>('Hello'); // calls Proc<T>(A: T); end;
Two non-instantiated generics are considered assignment compatible only if they are identical or are aliases to a common type.
Two instantiated generics are considered assignment compatible if the base types are identical (or are aliases to a common type) and the type arguments are identical.
Note: Generics in Delphi are unlike templates in C++ or generic types in C#. Most notably, a type parameter cannot be constrained to a specific simple type, such as Integer, Double, String, and so forth. In cases where different types need to be expressed (including simple types), consider using overloaded functions for each needed type or use a record type such as TValue from the System.Rtti unit, which offers operators and methods for storing and querying these data types.