type helper constructor

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

type helper constructor

Free Pascal - General mailing list
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: type helper constructor

Free Pascal - General mailing list
Am Do., 17. Jan. 2019, 13:10 hat Mattias Gaertner via fpc-pascal <[hidden email]> geschrieben:
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?

I would say it's a bug as in my opinion it should behave as with classes. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal