Delphi compiler evolves much faster than Delphi documentation, and some language features remain unnamed. Consider the following code snippet:
type MyPChar1 = PChar; MyPChar2 = type PChar; MyPChar3 = ^Char; procedure Test(Ch: PChar); begin end; procedure TForm1.Button4Click(Sender: TObject); var Ch1: MyPChar1; Ch2: MyPChar2; Ch3: MyPChar3; begin Test(Ch1); // Test(Ch2); Error E2008 Incompatible types // Test(Ch3); Error E2010 Incompatible types: 'Unit1.Char' and 'System.Char' end;
The documentation states that MyPChar1 and PChar are identical types, while MyPChar2 and MyPChar3 are distinct; that is why Test(Ch2); and Test(Ch3); lines does not compile. But notice – the compiler issues different error codes. Does it matter?
Consider the next snippet:
procedure Test1(Ch: MyPChar1);
begin
end;
procedure Test2(Ch: MyPChar2);
begin
end;
procedure Test3(Ch: MyPChar3);
begin
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Test1('Foo');
Test2('Foo'); // Compiles
// Test3('Foo'); Error E2010 Incompatible types: 'MyPChar3' and 'string'
end;
The documentation states that PChar type is assignment compatible on input with string literal. It appears that there is a difference in type ‘distinctness’. The type PChar type is somewhat more compatible with PChar than ^Char type.
From Help (Look For: type compatibility, data types):
RAD Studio (Common)
Type Compatibility and Identity
Type identity is almost straightforward. When one type identifier is declared using another type identifier, without qualification, they denote the same type. Thus, given the declarations
type
T1 = Integer;
T2 = T1;
T3 = Integer;
T4 = T2;
T1, T2, T3, T4, and Integer all denote the same type. To create distinct types, repeat the word type in the declaration. For example,
type TMyInteger = type Integer;
creates a new type called TMyInteger which is not identical to Integer.
…see more to Help
Use the Force, Luke!
Sorry, Your documentation link follow on same page for help topic.
type
MyPChar1 = PChar;
MyPChar2 = type PChar;
var
Ch1: MyPChar1;
Ch2: MyPChar2;
Ch3: MyPChar3;
TypeOf(Ch1) TypeOf(Ch2)
TypeOf(Ch1) NOT EQUAL TypeOf(Ch2)
Pingback: Delphi “type types”: similar types but not the same type identity, some examples. « The Wiert Corner – irregular stream of stuff