* * *

Author Topic: [SOLVED] Function as a parameter  (Read 450 times)

MountainQ

  • New member
  • *
  • Posts: 14
[SOLVED] Function as a parameter
« on: September 13, 2017, 04:38:38 pm »
Hi everyone,
on occasion I pass a reference to a function (here: 'f1') to anther function (here: 'f2').
Naturally the type of f1 has to be specified in the declaration of f2.
f1 can potentially be part of an object or inside another function, therefore its type changes (addition of 'of object'/'is nested' is required).
The implementation of f2 however is not affected by the type of f1.
Is it possible to adress several types of f1 with a single f2?
Overloading f2 is an easy solution; however as the implementations of f2 are identical, it appears somewhat verbose.
The reference for procedures

https://freepascal.org/docs-html/ref/refse17.html

does not mention anything.
Thanks in advance
« Last Edit: September 14, 2017, 10:58:46 am by MountainQ »

Thaddy

  • Hero Member
  • *****
  • Posts: 4433
Re: Function as a parameter
« Reply #1 on: September 13, 2017, 04:56:00 pm »
There's a huge difference between a function and a function of object. You can't exchange the two since the latter has a hidden self parameter.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

MountainQ

  • New member
  • *
  • Posts: 14
Re: Function as a parameter
« Reply #2 on: September 13, 2017, 05:04:55 pm »
Thank you Thaddy,
I agree; internally the function references are different; as they additionally contain a reference to the object or the scope of the enclosing function.
I was just unsure whether the compiler can automatically take care of that.
If there are no more thoughts on this issue I will close it today.
Once again thanks

Thaddy

  • Hero Member
  • *****
  • Posts: 4433
Re: Function as a parameter
« Reply #3 on: September 13, 2017, 05:13:17 pm »
If you can get away with a static class function, though.. (be careful) that has the same fingerprint as a normal function.... That might help.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 449
    • My "almost daily" development blog
Re: Function as a parameter
« Reply #4 on: September 13, 2017, 05:17:57 pm »
Well, why not... (pseudocode, may contain errors, or be very wrong :) @Thaddy knows I'm capable  O:-))
Code: Pascal  [Select]
  1. type bare_function = function: boolean;
  2. type object_function = function: boolean of object;
  3. type TFunction = class
  4.   f1: bare_function;
  5.   f2: object_function;
  6.   function Value: boolean;
  7. end;
  8.  
  9. function TFunction.value: boolean;
  10. begin
  11.   if f1 <> nil then Result := f1 else
  12.   if f2 <> nil then Result := f2 else
  13.     raise Exception.Create('error');
  14. end;
  15.  
  16. procedure PreformAction(f1: TFunction);
  17. begin
  18.   //do something fun with f1.Value
  19. end;
  20.  
  21. procedure Some_code_calling_the_function;
  22. var aFunction: TFunction;
  23.      link_bare: bare_function;
  24.      link_obj: object_function;
  25. begin
  26.   aFunction := TFunction.create;
  27.   aFunction.f1 := link_bare;
  28.   OR
  29.   aFunction.f2 := link_obj;
  30.  
  31.   PreformAction(aFunction);
  32.  
  33.   FreeAndNil(aFunction);
  34. end;
  35.  
  36.  
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

MountainQ

  • New member
  • *
  • Posts: 14
Re: Function as a parameter
« Reply #5 on: September 13, 2017, 05:28:42 pm »
You guys rock;
however I am afraid the provided solution would not make the code clearer. I will toy with this idea though..
Thanks to all.



Eugene Loza

  • Sr. Member
  • ****
  • Posts: 449
    • My "almost daily" development blog
Re: Function as a parameter
« Reply #6 on: September 13, 2017, 05:32:38 pm »
Oh... I've forgotten about overloading. That'll look much cleaner.
Code: Pascal  [Select]
  1. procedure MyFunction(f1: bare_function);
  2. var aFunction: TFunction;
  3. begin
  4.   aFunction := TFunction.create;
  5.   aFunction.f1 := f1;
  6.  
  7.   MainFunctionBody(aFunction);
  8.  
  9.   FreeAndNil(aFunction);
  10. end;
  11.  
  12. procedure MyFunction(f2: object_function);
  13. var aFunction: TFunction;
  14. begin
  15.   aFunction := TFunction.create;
  16.   aFunction.f2 := f2;
  17.  
  18.   MainFunctionBody(aFunction);
  19.  
  20.   FreeAndNil(aFunction);
  21. end;
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 449
    • My "almost daily" development blog
Re: Function as a parameter
« Reply #7 on: September 13, 2017, 05:35:26 pm »
And one more (very dirty) way to do this is to...
Code: Pascal  [Select]
  1. MYFUNCTION.inc
  2. function SomeFunction(f1: {$ifdef objfunction}object_fnction{$else}bare_function{$endif});
  3. begin
  4.   //do something fun with f1
  5. end;
and main unit:
Code: Pascal  [Select]
  1. {$Define objfunction}
  2. {$include MYFUNCTION.inc}
  3. {$undef objfunction}
  4. {$include MYFUNCTION.inc}
This will automatically create two absolutely identical overloaded functions SomeFunction. Thou you'll have to edit it in a separate editor tab.
« Last Edit: September 13, 2017, 05:38:44 pm by Eugene Loza »
My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 324
    • Lebeau Software
Re: Function as a parameter
« Reply #8 on: September 14, 2017, 02:45:13 am »
The implementation of f2 however is not affected by the type of f1.
Is it possible to adress several types of f1 with a single f2?

Have you considered using Generics/Templates?
« Last Edit: September 14, 2017, 02:48:00 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

MountainQ

  • New member
  • *
  • Posts: 14
Re: Function as a parameter
« Reply #9 on: September 14, 2017, 10:58:13 am »
Quote
Have you considered using Generics/Templates?
Thank you and yes, I did. Similarly to the above solution, this requires to wrap the function in a class. So it becomes a trade-off whether the added complexity outweighs the (probably) reduction in code lines.

I (partly) started this topic to find out whether a compiler setting could take care of that problem 'behind the scene'.
Best regards to everyone

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus