Recent

Author Topic: Advanced record constructors  (Read 3735 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Advanced record constructors
« on: June 07, 2020, 10:18:45 pm »
What is the essential difference between calling a record's constructor, and calling a 'standard' procedure of that record?
In other words, in what circumstances would you want to declare a constructor for an advanced record, rather than just declaring a plain procedure? And are such routines in records correctly termed 'methods', or is that term restricted to the routines of classes and interfaces?

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Advanced record constructors
« Reply #1 on: June 07, 2020, 11:05:34 pm »
Hello howardpc,

Perhaps you missed it because it is (officially) named extended records in Free Pascal, see https://www.freepascal.org/docs-html/ref/refse58.html#x118-1400009.1 and https://www.freepascal.org/docs-html/ref/refse59.html#x119-1410009.2

cheers

edit: and i just see that there is an error in that documentation. A constructor is allowed, as long as it is not parameterless.

« Last Edit: June 07, 2020, 11:33:09 pm by TRon »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advanced record constructors
« Reply #2 on: June 07, 2020, 11:31:41 pm »
What is the essential difference between calling a record's constructor, and calling a 'standard' procedure of that record?
In other words, in what circumstances would you want to declare a constructor for an advanced record, rather than just declaring a plain procedure? And are such routines in records correctly termed 'methods', or is that term restricted to the routines of classes and interfaces?

Maybe it behaves like OBJECTS do. the difference being one can have a Pointer referenced to it etc..

I don't Know, maybe I should experiment a bit  :D


EDIT:
  Must be something new, cause I can't do that in 3.0.4
« Last Edit: June 07, 2020, 11:34:01 pm by jamie »
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advanced record constructors
« Reply #3 on: June 07, 2020, 11:43:53 pm »
Interesting,,,,

When dynamically creating a Record using the NEW(MYRecordPointer, Can't call a Method of this recorc);

 The NEW does not support calling of procedures/functions of said dynamic record …..

  Should this be reported ? Really thing NEW should be able to do this.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Advanced record constructors
« Reply #4 on: June 07, 2020, 11:44:06 pm »
EDIT:
  Must be something new, cause I can't do that in 3.0.4
not sure what you are referring to exactly, but see https://wiki.freepascal.org/FPC_New_Features_2.6.0#Advanced_record_syntax

did you forgot the modeswitch perhaps ?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Advanced record constructors
« Reply #5 on: June 07, 2020, 11:45:54 pm »
The first page of documentation TRon referenced states:
Quote
Some of the restrictions when compared to classes or objects are obvious from the syntax diagram:

...

Constructors or destructors cannot be defined.
Yet this little program compiles and produces the expected output:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$ModeSwitch advancedrecords}
  5.  
  6. type
  7.   TPlainRec = record
  8.   public
  9.     procedure Init(const aText: String);
  10.   end;
  11.  
  12.  
  13.   TEnhancedRec = record
  14.   public
  15.     constructor Init(const aText: String);
  16.   end;
  17.  
  18. var
  19.   p: TPlainRec;
  20.   e: TEnhancedRec;
  21.  
  22. { TEnhancedRec }
  23.  
  24. constructor TEnhancedRec.Init(const aText: String);
  25. begin
  26.   WriteLn(aText);
  27. end;
  28.  
  29. procedure TPlainRec.Init(const aText: String);
  30. begin
  31.   WriteLn(aText);
  32. end;
  33.  
  34. begin
  35.   p.Init('plain');
  36.   e.Init('enhanced');
  37.   ReadLn;
  38. end.


jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advanced record constructors
« Reply #6 on: June 07, 2020, 11:59:41 pm »
I wasn't reading the message close enough, it was "non parametered " create not allowed.

well well. so I can't use a plain ole  constructor, it needs a parameter..
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Advanced record constructors
« Reply #7 on: June 08, 2020, 12:19:44 am »
@jamie
that it requires a parameter, make more or less sense to me.

You are indeed correct that you can't seem to use new and invoke the constructor at the same time ("Error: Pointer to object expected").

I wonder if that was done on purpose or that it was an oversight ?

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advanced record constructors
« Reply #8 on: June 08, 2020, 12:32:21 am »
I would say an oversight. The New isn't used much because people forget about it but I still use Objects because they work both ways, static or dynamic and for the most part I can do lots of things with objects and if I exit early I don't normally need to worry about cleanup in a block..
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Advanced record constructors
« Reply #9 on: June 08, 2020, 01:32:55 am »
Well its confirmed , Fpc has a bug with the NEW and constructors in a Record.. at least the 3.0.4 does..

 NEW(PointerTORecord, ConstructorofSaidRecord(A_Parameter)) refuses to compile..

This works in Delphi without an issue but it does insist on using an Constructor, not just any method..

 Can anyone else confirm this with higher level of compilers ?

Code: Pascal  [Select][+][-]
  1.   PMyType = ^TmyType;
  2.   TMyType = Record
  3.    Constructor init(A:Byte);
  4.   end;  
  5.   implementation
  6.  
  7. {$R *.lfm}
  8. Constructor TmyType.Init(A:Byte);
  9. Begin
  10.  if A = 1 Then Beep;
  11. End;
  12. { TForm1 }
  13. procedure TForm1.Button1Click(Sender: TObject);
  14. Var
  15.   P:PMyType;
  16. begin
  17.   New(P,init(1));  // This works in Delphi .Of course advancedRecords is on..
  18. end;                                      
  19.  
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Advanced record constructors
« Reply #10 on: June 08, 2020, 04:14:31 am »
oh, sorry. i thought i mentioned it already  :-[

3.2.0rc1 produced that error.

egsuh

  • Hero Member
  • *****
  • Posts: 1296
Re: Advanced record constructors
« Reply #11 on: June 08, 2020, 04:36:43 am »
AFAIK, constructors in records do nothing but setting initial values to the fields of the record.

Record variables p and e, in follwoing example, have their own local (or global? anyway not at memory heap) memory allocated at the definition.

Code: Pascal  [Select][+][-]
  1. var
  2.   p: TPlainRec;
  3.   e: TEnhancedRec;

The issue seems that record variable allocated by "new" procedure does not support class-like methods. Not sure about Delphi.

Thaddy

  • Hero Member
  • *****
  • Posts: 14380
  • Sensorship about opinions does not belong here.
Re: Advanced record constructors
« Reply #12 on: June 08, 2020, 09:14:19 am »
This is only allowed with old school objects and Delphi behaves the same.
The whole KOL library is based on it, and simply works.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Advanced record constructors
« Reply #13 on: June 08, 2020, 09:23:39 am »
What is the essential difference between calling a record's constructor, and calling a 'standard' procedure of that record?
In other words, in what circumstances would you want to declare a constructor for an advanced record, rather than just declaring a plain procedure? And are such routines in records correctly termed 'methods', or is that term restricted to the routines of classes and interfaces?

A constructor is essentially a class function. Though if jamie is right with his comment that in Delphi constructors can also be used for extended New then this would be a further difference to ordinary class methods and would additionally explain why the Delphi developers decided to use the constructor keyword.

And yes, the term method is also used here, essentially for any routine that has a hidden Self parameter (e.g. a static class function (compared to a class function) wouldn't be a method ;) ).

This works in Delphi without an issue but it does insist on using an Constructor, not just any method..

It makes sense that it only works with constructors (the same is true for the TP style objects), because then the compiler can be sure about the function signature.

ASerge

  • Hero Member
  • *****
  • Posts: 2246
Re: Advanced record constructors
« Reply #14 on: June 08, 2020, 07:09:50 pm »
Though if jamie is right with his comment that in Delphi constructors can also be used for extended New then
It's right. This simple project compiled and work in Delphi (at least 10.3), but not in FPC 3.0.4 or FPC 3.3.1:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$IFDEF FPC}
  3.   {$MODE OBJFPC}
  4.   {$MODESWITCH ADVANCEDRECORDS}
  5. {$ENDIF}
  6.  
  7. type
  8.   PMyType = ^TMyType;
  9.   TMyType = record
  10.     constructor Init(AValue: Integer);
  11.   end;
  12.  
  13. constructor TMyType.Init(AValue: Integer);
  14. begin
  15.   Writeln('Init ', AValue);
  16. end;
  17.  
  18. var
  19.   P: PMyType;
  20. begin
  21.   New(P, Init(123)); // or P := New(PMyType, Init(123));
  22.   Dispose(P);
  23.   Readln;
  24. end.

 

TinyPortal © 2005-2018