Recent

Author Topic: Anonymous methods/Closures  (Read 1152 times)

lazycat2

  • New member
  • *
  • Posts: 8
Anonymous methods/Closures
« on: July 04, 2022, 04:06:59 pm »
As FreePascal doesn't support anonymous methods/closures, what is the best way to write code like I posted below in FreePascal?

Code: Pascal  [Select][+][-]
  1. procedure DoSomethingInBackgroundThread;
  2. var
  3.   L1, L2: Integer;
  4. begin
  5.   TThread.Synchronize(procedure
  6.   begin
  7.     DoSomethingElse(L1, L2);
  8.   end);
  9. end;
  10.  

The obvious way I see is to use a TThread class, converting L1 and L2 to fields - but that means a lot of typing...

lazycat2

  • New member
  • *
  • Posts: 8
Re: Anonymous methods/Closures
« Reply #1 on: July 04, 2022, 04:16:35 pm »
Sorry posted in the wrong section, this was intended for General.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Anonymous methods/Closures
« Reply #2 on: July 04, 2022, 04:17:36 pm »
It is exactly right.  Anonymous methods are only supported in the development version trunk/main

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Anonymous methods/Closures
« Reply #3 on: July 04, 2022, 04:18:37 pm »
It is exactly right.  Anonymous methods are only supported in the development version trunk/main

Typical solution is a queue to hold L1, l2 tuples, and then .synchronize a method that drains the queue.

lazycat2

  • New member
  • *
  • Posts: 8
Re: Anonymous methods/Closures
« Reply #4 on: July 07, 2022, 05:53:28 pm »
Typical solution is a queue to hold L1, l2 tuples, and then .synchronize a method that drains the queue.
Interesting. Is this an existing feature in FPC/Lazarus?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anonymous methods/Closures
« Reply #5 on: July 14, 2022, 08:04:29 am »
It is exactly right.  Anonymous methods are only supported in the development version trunk/main

Typical solution is a queue to hold L1, l2 tuples, and then .synchronize a method that drains the queue.
Why not simply put L1 and L2 into unit level?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Anonymous methods/Closures
« Reply #6 on: July 14, 2022, 08:44:41 am »
Typical solution is a queue to hold L1, l2 tuples, and then .synchronize a method that drains the queue.
Interesting. Is this an existing feature in FPC/Lazarus?

Not in its totality, but the necessary building blocks are there.

It is exactly right.  Anonymous methods are only supported in the development version trunk/main

Typical solution is a queue to hold L1, l2 tuples, and then .synchronize a method that drains the queue.
Why not simply put L1 and L2 into unit level?

What if you have multiple threads or multiple invocations of DoSomethingInBackgroundThread? In context of threading global variables are nearly always the wrong answer.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anonymous methods/Closures
« Reply #7 on: July 14, 2022, 09:35:55 am »
What if you have multiple threads or multiple invocations of DoSomethingInBackgroundThread? In context of threading global variables are nearly always the wrong answer.
Ah, OK. That makes sense.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Anonymous methods/Closures
« Reply #8 on: July 14, 2022, 12:45:03 pm »
Why not simply put L1 and L2 into unit level?

What if you have multiple threads or multiple invocations of DoSomethingInBackgroundThread? In context of threading global variables are nearly always the wrong answer.

Or in my case, I had slow UI components like TChart in applications that had 3 cameras x 40fps.

 

TinyPortal © 2005-2018