Recent

Author Topic: [SOLVED] nestedprocvars in ObjFPC mode?  (Read 1473 times)

Key-Real

  • Sr. Member
  • ****
  • Posts: 404
[SOLVED] nestedprocvars in ObjFPC mode?
« on: March 20, 2022, 05:52:01 pm »
This works only in delphi mode.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$modeswitch nestedprocvars}
  3. unit test;
  4. interface
  5. type
  6.     TtuiProc = procedure;
  7.  
  8. procedure itemIteration(ButtonProc:TtuiProc;TextfieldProc:TtuiProc);
  9.  
  10. function updateBox:boolean;
  11.  
  12. implementation
  13.  
  14. function updateBox:boolean;
  15.  
  16.     procedure updateButtonProc;
  17.     begin
  18.     end;
  19.  
  20.     procedure updateTextfieldProc;
  21.     begin
  22.     end;
  23.  
  24. begin
  25.  itemIteration(@updateButtonProc,@updateTextfieldProc);
  26. end;
  27.  
  28.  
  29.  
  30. procedure itemIteration(ButtonProc:TtuiProc;TextfieldProc:TtuiProc);
  31. begin
  32.  ButtonProc;
  33.  TextfieldProc;
  34. end;
  35.  
  36. begin
  37. end.
  38.  


I need this feature in objfpc mode.

Is it a good idea to include my suggestion this in a next version of fpc?
« Last Edit: March 20, 2022, 07:04:40 pm by Key-Real »

Thaddy

  • Hero Member
  • *****
  • Posts: 19262
  • Glad to be alive.
Re: nestedprocvars in ObjFPC mode?
« Reply #1 on: March 20, 2022, 05:54:03 pm »
No, it is already fully supported. Also in mode objfpc. So where's the problem exactly? And what does the compiler say?
(Let me guess: ItemIteration has the wrong Params for mode objfpc compared to mode delphi)
« Last Edit: March 20, 2022, 05:58:55 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Key-Real

  • Sr. Member
  • ****
  • Posts: 404
Re: nestedprocvars in ObjFPC mode?
« Reply #2 on: March 20, 2022, 06:10:24 pm »
If I change the code to {$mode objfpc} The compiler say:

Free Pascal Compiler version 3.3.1 [2022/03/12] for aarch64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Darwin for AArch64
Compiling test.pas
test.pas(27,54) Error: Incompatible type for arg no. 2: Got "<procedure variable type of procedure is nested;StdCall>", expected "<procedure variable type of procedure;StdCall>"
test.pas(40,4) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted



Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: nestedprocvars in ObjFPC mode?
« Reply #3 on: March 20, 2022, 06:27:04 pm »
In Delphi mode, like in Delphi, @proc evaluates to an untyped pointer. In ObjFPC mode, @proc is the same as "proc" in Delphi mode: it evaluates to a procvar type with the signature of the passed proc. To change that into an untyped pointer in ObjFPC mode, you have to add a pointer() typecast. However, this completely eliminates type checking, which is a bad idea.

The reason is that this only works in your program if updateButtonProc and updateTextfieldProc do not access local variables or parameters from updateBox. If they do, you have to change the declaration of TtuiProc to "TtuiProc = procedure is nested;". Even when they don't to this, it's better to use this correct type so you don't need the @-hack in Delphi mode to get rid of type checking, nor the pointer() hack in ObjFPC mode.

Key-Real

  • Sr. Member
  • ****
  • Posts: 404
Re: nestedprocvars in ObjFPC mode?
« Reply #4 on: March 20, 2022, 07:03:53 pm »
Thx, now ist works!

Var myProc = Procedure is Nested;

Done the Job.

 

TinyPortal © 2005-2018