Recent

Author Topic: Assigning proc to event at run time  (Read 2815 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 362
Assigning proc to event at run time
« on: January 11, 2025, 10:43:01 pm »
Code: Pascal  [Select][+][-]
  1. procedure Twirl(Sender: TObject);    
  2.  
  3. EditRadius.OnChange:= @Twirl;    
  4.  

This works only if both twirl and OnChange have same parameters.

Can this assignment be done without that condition (both having same parameters) ?

Tested, at design time I can put Twirl at onChange event, regardles if it has parameter/s or not.
I think it complains a bit, but accepts anyway and works.
« Last Edit: January 11, 2025, 10:45:21 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Assigning proc to event at run time
« Reply #1 on: January 11, 2025, 10:58:53 pm »
Code: Pascal  [Select][+][-]
  1. {$modeswitch anonymousfunctions}
  2. {$modeswitch functionreferences}
  3.  
  4. // ...
  5.  
  6. EditRadius.OnChange := procedure(Sender: TObject)
  7. begin
  8.   // do whatever you want, call any procedure you want
  9.   Twirl;
  10. end;

alpine

  • Hero Member
  • *****
  • Posts: 1410
Re: Assigning proc to event at run time
« Reply #2 on: January 11, 2025, 11:14:22 pm »
Shouldn't it be procedure(...) of object?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Assigning proc to event at run time
« Reply #3 on: January 11, 2025, 11:27:03 pm »
Yes, of object.
An event expects a hidden self parameter and that is what of object provides.
(You do not have to actually use it, but it must be there.)
« Last Edit: January 11, 2025, 11:29:28 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6238
  • Compiler Developer
Re: Assigning proc to event at run time
« Reply #4 on: January 13, 2025, 09:16:09 pm »
Code: Pascal  [Select][+][-]
  1. procedure Twirl(Sender: TObject);    
  2.  
  3. EditRadius.OnChange:= @Twirl;    
  4.  

This works only if both twirl and OnChange have same parameters.

Can this assignment be done without that condition (both having same parameters) ?

The compiler has strong typing for a reason. A method/function passed to a method/function pointer with a different signature will lead to crashes depending on the parameters and the calling convention / platform involved.

Tested, at design time I can put Twirl at onChange event, regardles if it has parameter/s or not.
I think it complains a bit, but accepts anyway and works.

It's simply that the compiler can't check at compile time whether everything is correct in the LFM file, because it knows nothing about LFM files. This will break depending on the parameters and the calling convention / platform involved.

Don't do this.

Bart

  • Hero Member
  • *****
  • Posts: 5649
    • Bart en Mariska's Webstek
Re: Assigning proc to event at run time
« Reply #5 on: January 13, 2025, 10:16:59 pm »
It's simply that the compiler can't check at compile time whether everything is correct in the LFM file, because it knows nothing about LFM files. This will break depending on the parameters and the calling convention / platform involved.

Don't do this.

Indeed don't.
There is a bugreport about Lazarus allowing this/not warning on loading the lfm.
It caused a crash in the reporter's application.

Bart

 

TinyPortal © 2005-2018