Recent

Author Topic: Passing parameters by name  (Read 579 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2073
Passing parameters by name
« on: January 19, 2021, 11:19:29 pm »
Can somebody provide an update on defining records using named fields at runtime? I believe that this was supposed to be implemented at some point but I've just been idly experimenting and it doesn't (yet) work.

Or I'm doing something stupid, which is of course likely :-)

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. type
  4.   opType= record
  5.             name: string[255];
  6.             priority: integer
  7.           end;
  8.  
  9. const
  10.   add: opType= (name: '+'; priority: 5);   // OK
  11. //  sub: opType= (priority: 5; name: '-'); // Compile-time error (as documented)
  12.   mul: opType= (name: '*');                // Compile-time warning
  13.  
  14.  
  15. procedure defineOp(const op: opType);
  16.  
  17. begin
  18. end { defineOp } ;
  19.  
  20.  
  21. begin
  22.   defineOp(add);                   // OK
  23.   defineOp(name: '+'; priority: 5) // Compile-time error
  24. end.
  25.  

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

josh

  • Hero Member
  • *****
  • Posts: 808
Re: Passing parameters by name
« Reply #1 on: January 20, 2021, 12:17:42 am »
 
Code: [Select]
mul: opType= (name: '*');if you add your priority param
Code: [Select]
mul: opType= (name: '*'; priority: 2);
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2751
  • Compiler Developer
Re: Passing parameters by name
« Reply #2 on: January 20, 2021, 08:19:17 am »
Can somebody provide an update on defining records using named fields at runtime? I believe that this was supposed to be implemented at some point but I've just been idly experimenting and it doesn't (yet) work.

This is not supported and there are no plans to do so.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2073
Re: Passing parameters by name
« Reply #3 on: January 20, 2021, 10:28:51 am »
Code: [Select]
mul: opType= (name: '*');if you add your priority param
Code: [Select]
mul: opType= (name: '*'; priority: 2);

Yes. But the documentation was unclear whether a missing field was a compilation or runtime error.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2073
Re: Passing parameters by name
« Reply #4 on: January 20, 2021, 10:33:01 am »
Can somebody provide an update on defining records using named fields at runtime? I believe that this was supposed to be implemented at some point but I've just been idly experimenting and it doesn't (yet) work.

This is not supported and there are no plans to do so.

OK. My mistake, I thought you'd said in the past that this sort of tuple would eventually be supported.

Unfortunate decision IMO, since I've had situations in the past where changing the number of parameters of a function with a final optional parameter resulted in a bug which was very difficult to pin down, and being able to pass parameters by name rather than position would avoid that.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2751
  • Compiler Developer
Re: Passing parameters by name
« Reply #5 on: January 20, 2021, 01:30:03 pm »
Can somebody provide an update on defining records using named fields at runtime? I believe that this was supposed to be implemented at some point but I've just been idly experimenting and it doesn't (yet) work.

This is not supported and there are no plans to do so.

OK. My mistake, I thought you'd said in the past that this sort of tuple would eventually be supported.

There was the suggestion by Ryan Joseph to add support for record constructors that are similar to the variable/constant initializations. And maybe I said that this might be investigated.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2073
Re: Passing parameters by name
« Reply #6 on: January 20, 2021, 01:35:14 pm »
There was the suggestion by Ryan Joseph to add support for record constructors that are similar to the variable/constant initializations. And maybe I said that this might be investigated.

Maybe. This might have been about three years ago.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

 

TinyPortal © 2005-2018