Recent

Author Topic: Parametrized Maco's?  (Read 731 times)

jamie

  • Hero Member
  • *****
  • Posts: 7329
Parametrized Maco's?
« on: October 04, 2025, 11:46:55 pm »
Is it possible to have this option?

Code: Pascal  [Select][+][-]
  1. {$MACRO on}
  2. {$DefineFunc Clear(AParam)= FillByte(AParm,SizeOF(Aparam),0);
  3.  
  4.  

And somewhere in code;

Code: Pascal  [Select][+][-]
  1. Var
  2. A:TSomeREcord;
  3. Begin
  4. Clear(A);
  5.  

That would solve the copy problem of clearing memory with a short syntax.

Jamie
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11828
  • Debugger - SynEdit - and more
    • wiki
Re: Parametrized Maco's?
« Reply #1 on: October 04, 2025, 11:53:26 pm »
IIRC been discussed/asked several times and always rejected.

But you can use generic functions afaik (not sure if it needs trunk)

from memory / not tested
Code: Pascal  [Select][+][-]
  1. generic function clear<A>(data: A);
  2. begin
  3.   fillbyte(data, sizeof(A);
  4. end;

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Parametrized Maco's?
« Reply #2 on: October 04, 2025, 11:56:18 pm »
But I have to pass 2 items, not one if I do that.

Clear(Type, Instance);
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11828
  • Debugger - SynEdit - and more
    • wiki
Re: Parametrized Maco's?
« Reply #3 on: October 05, 2025, 12:26:40 am »
Well, not in trunk. https://forum.lazarus.freepascal.org/index.php?topic=59124.0

This compiles. (I haven't tested it)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$Mode objfpc}
  3. {$ModeSwitch ImplicitFunctionSpecialization }
  4.  
  5. generic procedure clear<A>(data: A); inline;
  6. begin
  7.   fillbyte(data, sizeof(A), 0);
  8. end;
  9.  
  10. type
  11.   TFoo = record a,b,c: qword; end;
  12. var
  13.   f: tfoo;
  14. begin
  15.   Clear(f);
  16. end.
  17.  

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Parametrized Maco's?
« Reply #4 on: October 05, 2025, 12:37:15 am »
That would be nice but Its trunk only and it looks like it's not DELPHI compliant, but who's counting!

 :o
The only true wisdom is knowing you know nothing

korba812

  • Sr. Member
  • ****
  • Posts: 482
Re: Parametrized Maco's?
« Reply #5 on: October 05, 2025, 01:21:29 am »
...it looks like it's not DELPHI compliant
Just like macros

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 302
  • I use FPC [main] 💪🐯💪
Re: Parametrized Maco's?
« Reply #6 on: October 05, 2025, 05:54:04 am »
looks like it's not DELPHI compliant

Here is a compatible solution:
Code: Pascal  [Select][+][-]
  1. program app;
  2. {$ifdef FPC}
  3. {$mode delphi}
  4. {$modeSwitch implicitfunctionspecialization}
  5. {$endif}
  6.  
  7. type
  8.   TUtils = class
  9.     class procedure Clear<A>(data: A); static; inline;
  10.   end;
  11.  
  12. class procedure TUtils.Clear<A>(data: A);
  13. begin
  14.   FillChar(data, sizeof(A), 0);
  15. end;
  16.  
  17. type
  18.   TFoo = record a,b,c: UInt64; end;
  19.  
  20. var
  21.   f: TFoo;
  22.  
  23. begin
  24.   TUtils.Clear(f);
  25. end.
I may seem rude - please don't take it personally

runewalsh

  • Full Member
  • ***
  • Posts: 106
Re: Parametrized Maco's?
« Reply #7 on: October 05, 2025, 10:12:10 am »
Parametrized macros can be emulated with:

Code: Pascal  [Select][+][-]
  1. {$define Clear :=
  2.         FillByte(WhatToClear,SizeOF(WhatToClear),0); {$undef WhatToClear}}
  3. Var
  4.         A: TSomeREcord;
  5. Begin
  6.         {$define WhatToClear := A} Clear
  7.  

Highly discouraged, don’t do that... or you’ll end up with (my actual code for binary search):

Code: Pascal  [Select][+][-]
  1. // Inlineable binary search, similar to std::partition_point in C++.
  2.  
  3. {$define partition_vars :=
  4.         ptn_L, ptn_R, test_index: SizeUint;
  5.         partition_pos: SizeUint absolute ptn_L;}
  6.  
  7. {$define partition_impl :=
  8.         ptn_L := {$ifdef left} left {$else} 0 {$endif}; ptn_R := right;
  9.         while ptn_L < ptn_R do
  10.         begin
  11.                 test_index := SizeUint(ptn_L + ptn_R) div 2;
  12.                 if test then ptn_L := test_index + 1 else ptn_R := test_index;
  13.         end; {$undef test} {$undef left} {$undef right}}
  14.  
  15. // usage example; also a callback version for non-demanding cases.
  16. class function Partition.Find(L, R: SizeUint; test: Predicate; param: pointer): SizeUint;
  17. var
  18.         partition_vars
  19. begin
  20.         {$define left := L} {$define right := R} {$define test := {$macro off}test{$macro on}(test_index, param)}
  21.         partition_impl
  22.         result := partition_pos;
  23. end;
  24.  

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Parametrized Maco's?
« Reply #8 on: October 05, 2025, 10:19:35 am »
Implicit function specialization is Delphi compatible. (Higher versions > Berlin)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6195
  • Compiler Developer
Re: Parametrized Maco's?
« Reply #9 on: October 06, 2025, 02:33:31 pm »
Implicit function specialization is Delphi compatible. (Higher versions > Berlin)

Though Delphi does not support free-standing generic functions unlike FPC. ;)

 

TinyPortal © 2005-2018