Recent

Author Topic: procedure of object of object?  (Read 3627 times)

Edson

  • Hero Member
  • *****
  • Posts: 1302
procedure of object of object?
« on: February 23, 2017, 06:11:10 pm »
I would like yo use a event handler declared nested in a procedure:

Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2.  
  3.   procedure Bar;
  4.   begin
  5.   end;
  6.  
  7. begin
  8.   ...
  9.   SomeObject.OnEvent := @Bar;  //????
  10.   ...
  11. end;

Is it possible? This doesn't work: TMyEvent = procedure of object of object;
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: procedure of object of object?
« Reply #1 on: February 23, 2017, 06:13:35 pm »

Code: Pascal  [Select][+][-]
  1.  {$modeswitch nestedprocvars}
  2. type teventtype = procedure is nested;

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: procedure of object of object?
« Reply #2 on: February 23, 2017, 06:35:40 pm »

Code: Pascal  [Select][+][-]
  1.  {$modeswitch nestedprocvars}
  2. type teventtype = procedure is nested;

Thanks. Sometimes FPC surprises me.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: procedure of object of object?
« Reply #3 on: February 23, 2017, 07:36:53 pm »
Note that this is very dangerous. As soon as the outer procedure is exited, the procedural variable (which is the generic term for what you call an event handler) will become invalid. The reason is a nested procedure has access to the context of the outer procedure, and when the outer procedure exits this context gets destroyed (and hence becomes invalid).

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: procedure of object of object?
« Reply #4 on: February 23, 2017, 09:44:53 pm »
OK. I see it's dangerous. Maybe only useful for events of local objects.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018