Recent

Author Topic: Default parms in a Record constructor not allowed ?  (Read 1173 times)

jamie

  • Hero Member
  • *****
  • Posts: 6587
Default parms in a Record constructor not allowed ?
« on: August 17, 2024, 04:11:12 pm »
Code: Pascal  [Select][+][-]
  1. TArecord = Record
  2.  
  3.   //some fields;
  4.  Constructor create(xxx:integer=0; yyy:integer=0);
  5.  
  6.  

The above generates  a compiler error indicating that
"Error Parameterless constructors are not allowed in records or records/type helpers"

 What's wrong with the defaults? They are parameters are they not?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: Default parms in a Record constructor not allowed ?
« Reply #1 on: August 17, 2024, 05:11:26 pm »
Record constructors do not accept defaults.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$modeswitch advancedrecords}
  2. type
  3.   TsomeRecord = record
  4.     a,b:integer;
  5.     constructor create(a1,b1:integer);
  6.   end;
  7.   constructor TSomeRecord.create(a1,b1:integer);
  8.   begin
  9.     self.a := a1;
  10.     self.b := b1;
  11.   end;
  12. var S:TSomeRecord;
  13. begin
  14.   S:= TSomerecord.Create(100,110);
  15.   writeln (S.a);
  16.   writeln(s.b)
  17. end.
You can overcome that by implementing class operator TSomeRecord.initialize(var value:TSomeRecord);
« Last Edit: August 17, 2024, 05:39:20 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5690
  • Compiler Developer
Re: Default parms in a Record constructor not allowed ?
« Reply #2 on: August 18, 2024, 11:46:28 am »
What's wrong with the defaults? They are parameters are they not?

The point of that message is necessarily the declaration, but how it's going to be called. Delphi (which is the origin for this feature) simply does not allow TMyRec.Create; and if you have only default parameters this is a possibility to do. FPC adheres to this restriction.

So you either need to have at least one non-default parameter or you need to use a class function (there isn't really that much of a difference between a constructor and a class function for records).

Record constructors do not accept defaults.

Record constructors do allow default parameters. It's just that you can't have only default parameters.

jamie

  • Hero Member
  • *****
  • Posts: 6587
Re: Default parms in a Record constructor not allowed ?
« Reply #3 on: August 19, 2024, 03:20:14 am »
I resorted to using a member function as a constructor and it returns itself on that too, if desired, otherwise I can initiate it directly from the declaration instead of the Type to do so.


 basically I use the SELF as a pointer to return itself. works out fine.

Thanks,

The only true wisdom is knowing you know nothing

ASBzone

  • Hero Member
  • *****
  • Posts: 713
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Default parms in a Record constructor not allowed ?
« Reply #4 on: August 19, 2024, 06:06:48 am »
The point of that message is necessarily the declaration, ...

Just as a point of clarification, PascalDragon, was this supposed to include a not?  As in "not necessarily" ?
-ASB: https://www.BrainWaveCC.com/

Lazarus v3.5.0.0 (2216170cde) / FPC v3.2.3-1387-g3795cadbc8
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

ASerge

  • Hero Member
  • *****
  • Posts: 2324
Re: Default parms in a Record constructor not allowed ?
« Reply #5 on: August 19, 2024, 03:53:36 pm »
...basically I use the SELF as a pointer to return itself. works out fine.
In a record? Self is not a pointer.

By the way, the constructor for records is syntactic sugar to call the constructor as a function, and actually is not needed.
FPC converts "Instance := TRecord.Create;" into:
Code: Pascal  [Select][+][-]
  1. var Temp: TRecord;
  2.   //...
  3. Temp.Create;
  4. Instance := Temp;
If TRecord contains managed types, then additionally Initialize and Finalize;

Zvoni

  • Hero Member
  • *****
  • Posts: 2692
Re: Default parms in a Record constructor not allowed ?
« Reply #6 on: August 19, 2024, 04:18:02 pm »
Expanding Thaddy's sample
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$modeswitch advancedrecords}
  3. type
  4.   TsomeRecord = record
  5.     a:integer;
  6.     b:Integer;
  7.     class operator Initialize(var tsr:TSomeRecord);
  8.     constructor create(a1,b1:Integer);
  9.   end;
  10. Const TDefRec : TsomeRecord =(a: 41;b:42);   //Defaults
  11.  
  12. class operator TsomeRecord.Initialize(var tsr: TSomeRecord);
  13. begin
  14.   tsr:=TDefRec;
  15. end;
  16.  
  17. constructor TsomeRecord.create(a1, b1: Integer);
  18. begin
  19.   self.a := a1;
  20.   self.b := b1;
  21. end;
  22.  
  23. var S,T:TSomeRecord;
  24. begin
  25.   S:= TSomerecord.Create(100,110);
  26.   writeln (S.a);   //Writes 100
  27.   writeln(s.b);    //writes 110
  28.  
  29.   Writeln(T.a);    //writes 41
  30.   Writeln(T.b);    //writes 42
  31. end.
  32.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 6587
Re: Default parms in a Record constructor not allowed ?
« Reply #7 on: August 19, 2024, 11:10:04 pm »
This is basically what I did to solve it for the time being.
Take not on how I can initiate the records either way inline, etc. :o

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch AdvancedRecords}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.  { TMyRecord }
  14.  
  15.  TMyRecord = Record
  16.    X,Y:Integer;
  17.  Private
  18.  Function Create(aX:Integer=0;aY:integer=0):TmyRecord;
  19.  Property T[xx,yy:integer]:TMyRecord Read Create;  Default;
  20.  end;
  21.  
  22.   { TForm1 }
  23.  
  24.   TForm1 = class(TForm)
  25.     Button1: TButton;
  26.     procedure Button1Click(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TMyRecord }
  41.  
  42. function TMyRecord.Create(aX: Integer; aY: integer): TmyRecord;
  43. begin
  44.   X:=aX; Y:= aY;
  45.   Result := Self;
  46. end;
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51. Var
  52.   T:TMyRecord;
  53. begin
  54.   caption := T.Create(10,20).X.Tostring+','+T[10,20].Y.ToString; //Prints 10,20
  55. end;
  56.  
  57. end.
  58.  
  59.  
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018