Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-01

type helper constructor

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

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

Sven Barth
to Mattias Gaertner

Am Do., 17. Jan. 2019, 13:10 hat Mattias Gaertner via fpc-pascal <

Show quoted text 34 lines
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