Overloads and Type Compatibility in Generics

From RAD Studio
Jump to: navigation, search

Go Up to Generics Index

Overloads

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.

For example:

type
  TFoo = class
    procedure Proc<T>(A: T);
    procedure Proc(A: String);
    procedure Test;
  end;

procedure TFoo.Test;
begin
  Proc('Hello'); // calls Proc(A: String);
  Proc<String>('Hello'); // calls Proc<T>(A: T);
end;

Type Compatibility

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.

See Also

Personal tools
Newest Version: XE
In other languages