Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type promotion of overloaded function's argument

Consider the following code:

program Promote;

{$APPTYPE CONSOLE}

uses
  SysUtils;

{$HINTS OFF}

type
  MyWord = record
    FValue: LongWord;
    class operator Implicit(AValue: LongWord): MyWord;
  end;

class operator MyWord.Implicit(AValue: LongWord): MyWord;
begin
  Result.FValue:= AValue;
end;

procedure Test(AValue: MyWord); overload;
begin
  Writeln('MyWord');
end;

procedure Test(AValue: UInt64); overload;
begin
  Writeln('UInt64');
end;

var
  LW: LongWord;

begin
  Test(LW);
  Readln;
end.

When I run it (Delphi XE) I see that the compiler promotes LongWord parameter to UInt64 (built-in type), not to MyWord (user-defined type).

Can I assume that Delphi compiler always promotes a built-in type to a built-in type if such promotion is implemented in the compiler itself?

More generally what rules control type promotion in such situations (say we have 2 built-in types, or 2 user-defined types, etc)?

like image 534
kludg Avatar asked Jan 31 '15 06:01

kludg


1 Answers

To my understanding an ordinal type like uint64 will always be closer to another ordinal type like longword, than a record. Period.

The "closer type" distance is not about "built-in" or "custom" types. You are comparing apples and oranges.

In the compiler, records and ordinals are two diverse families. The fact you can define implicit conversion would never promote a record to become an ordinal type.

As a result, a "built-in" string will always have less affinity with an integer than a new ordinal type like:

type TMyInteger64 = type Int46;

Here, this TMyInteger64 type will be "nearer" than a built-in string type.

As soon as you define a record, it will have affinities with other record, not ordinal types.

like image 57
Arnaud Bouchez Avatar answered Nov 07 '22 01:11

Arnaud Bouchez