Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do angle brackets after a class name mean in a variable declaration?

Tags:

syntax

delphi

I have been programming in Delphi for a while, but I never ran across a syntax that I found in a question here on SO. There was this syntax:

var Dic: TDictionary<Integer,string>;

I've never seen the <type, type>. What does it mean? When and where can it be used? I found nothing, since Google omits chars like '<', '>'.

like image 794
Martin Melka Avatar asked Dec 09 '22 01:12

Martin Melka


2 Answers

It is the syntax used for generics. Generics allow you to define classes that are parameterized by type.

You can read all about it in the Delphi documentation. You may also find the Wikipedia page useful. It gives a broader overview of the concept of generic programming.

like image 168
David Heffernan Avatar answered Feb 16 '23 00:02

David Heffernan


In many languages, it's usually a mapping, or template instantiation, Delphi calls these generics, and an example of declaring them can be seen here:

type
  TPair<Tkey,TValue> = class   // TKey and TValue are type parameters
    FKey: TKey;
    FValue: TValue;
    function GetValue: TValue;
  end;

function TPair<TKey,TValue>.GetValue: TValue;
begin
  Result := FValue;
end;

What your particular example is defining is a dictionary that will map integers to strings.

like image 43
paxdiablo Avatar answered Feb 16 '23 01:02

paxdiablo