What does `type` keyword actually means when used in a type definition?
Consider the following code, and note that the procedure Check()
has a var
parameter:
type
Ta = string; // type alias
Tb = type string; // compatible but distinct new type
procedure Check(var s: string);
begin
ShowMessage(s);
end;
procedure TMain.Button2Click(Sender: TObject);
var
a: Ta;
b: Tb;
begin
a := 'string of type Ta,';
b := 'string of type Tb.';
Check(a);
Check(b);
end;
Check(b)
results in a compiler error:
E2033 Types of actual and formal var parameters must be identical
In the above, type Tb
is compatible with string
in that you can f. ex. assign a := b
, but it is distinct in that the type identifier
(under the hood) has a different value, and therefore not accepted as argument for Check(var s: string)
.
Type declaration like
TCaption = type string;
creates new type with different RTTI information. Also it cannot be used as var
parameter of function if string
type needed.
New RTTI information "...ensures that at run time, variables of this type are identified by their new type name...". So if you try to get type name for an instance of TCaptionSame = string;
, you'll get string
, while for TCaption
type variable you'll get TCaption
To get more exact information, it would better to refer to official help