[fpc-pascal] type helper constructor

Mattias Gaertner nc-gaertnma at netcologne.de
Thu Jan 17 13:10:25 CET 2019


Hi,

A type helper for a string can add a constructor.
But how it can be used differs between FPC and Delphi.

For example:

{$modeswitch typehelpers}
type 
  THelper = type helper for string
    constructor Creator(i: integer);
  end;

var
  s: string;
begin
  string.creator(3); 
  // allowed in delphi+fpc, creating a new string
  
  s.creator(3);
  // fpc: same as above, creating a new string
  // delphi: error: Cannot call constructors using
  //         instance variables
end;

IMO the fpc way is inconsistent to how constructors work for classes
and records. A constructor usually only creates something when called
with the type (e.g. TObject.Create), while behaving like a normal
function when called with an instance (e.g. obj.create).

Is this a bug or by design?

Mattias



More information about the fpc-pascal mailing list