Why can't I use class operator Implicit to convert to a static array of char

93 Views Asked by At

Using Delphi 11.

I can use class operator implicit to convert to a dynamic array of characters. I can also use it to convert to a static array of integers or strings, or just about any other type. But I can't use it to convert to a static array of characters. What am I missing?

type
  tStaticArrayOfInteger = array [ 1 .. 2 ] of integer;
  tStaticArrayOfString  = array [ 1 .. 2 ] of string;
  tStaticArrayOfChar    = array [ 1 .. 2 ] of char;   // <-  Trouble maker
  tDynamicArrayOfChar   = array of char;

type
  tThing = record
    private
      fIgnoreThisValue : boolean;
    public
      class operator Implicit ( const X : tThing ) : tStaticArrayOfInteger;
      class operator Implicit ( const X : tThing ) : tStaticArrayOfString;
      class operator Implicit ( const X : tThing ) : tStaticArrayOfChar;
      class operator Implicit ( const X : tThing ) : tDynamicArrayOfChar;
  end;

class operator tThing.Implicit ( const X : tThing ) : tStaticArrayOfInteger;
begin
  Result [1] :=  8;
  Result [2] := 21;
end;

class operator tThing.Implicit ( const X : tThing ) : tStaticArrayOfString;
begin
  Result [1] := 'hello';
  Result [2] := 'world';
end;

class operator tThing.Implicit ( const X : tThing ) : tStaticArrayOfChar;
begin
  Result [1] := 'H';
  Result [2] := 'W';
end;

class operator tThing.Implicit ( const X : tThing ) : tDynamicArrayOfChar;
begin
  setlength ( Result, 2 );
  Result [0] := 'h';
  Result [1] := 'w';
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  aThing                : tThing;
  aStaticArrayOfInteger : tStaticArrayOfInteger;
  aStaticArrayOfString  : tStaticArrayOfString;
  aStaticArrayOfChar    : tStaticArrayOfChar;
  aDynamicArrayOfChar   : tDynamicArrayOfChar;
begin
  aThing . fIgnoreThisValue := false;
  aStaticArrayOfInteger := aThing; // This works fine
  aStaticArrayOfString  := aThing; // This works fine

  aStaticArrayOfChar    := aThing; // <-- Incompatible types:
                                   // 'tStaticArrayOfChar' and 'tThing'

  aDynamicArrayOfChar   := aThing; // This works fine
end;
0

There are 0 best solutions below