Recent

Author Topic: Anonymous Methods  (Read 43354 times)

Fiji

  • Guest
Anonymous Methods
« on: March 13, 2014, 12:52:23 pm »
How to use this with the latest build of FPC? This would make use of Omni Thread Library possible.

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/anonymousmethods_xml.html

Thanks
« Last Edit: March 13, 2014, 12:57:52 pm by Fiji »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12630
  • FPC developer.
Re: Anonymous Methods
« Reply #1 on: March 13, 2014, 01:57:06 pm »
Afaik anonymous methods are being looked at by a developer, but not in trunk yet.

avra

  • Hero Member
  • *****
  • Posts: 2582
    • Additional info
Re: Anonymous Methods
« Reply #2 on: March 13, 2014, 03:36:32 pm »
How to use this with the latest build of FPC? This would make use of Omni Thread Library possible.
... and probably OpenWire. Mitov dumped Lazarus support because of latest Delphi features (XE2+) not available in FPC.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Anonymous Methods
« Reply #3 on: March 13, 2014, 03:42:35 pm »
I'm going to need a good example of anonymous method usage to get convinced that it has any merit over inlined or local procedures. I heard a lot of people praise them but I haven't found any example that shows the benefit of using them. Any one has such examples preferably from real projects not just "sample to prove a point" pieces of code or if you do those be prepared to get shot.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Fiji

  • Guest
Re: Anonymous Methods
« Reply #4 on: March 13, 2014, 03:47:51 pm »
How to use this with the latest build of FPC? This would make use of Omni Thread Library possible.
... and probably OpenWire. Mitov dumped Lazarus support because of latest Delphi features (XE2+) not available in FPC.

He dumped support for lazarous? When was that? This feature is in Delphi since 2009. It makes multi-threading very very easy among other things..

Here is a webinar from Mitov:

https://www.youtube.com/watch?v=7g-dmfSH6ZU

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12630
  • FPC developer.
Re: Anonymous Methods
« Reply #5 on: March 13, 2014, 03:59:40 pm »
I'm going to need a good example of anonymous method usage to get convinced that it has any merit over inlined or local procedures. I heard a lot of people praise them but I haven't found any example that shows the benefit of using them. Any one has such examples preferably from real projects not just "sample to prove a point" pieces of code or if you do those be prepared to get shot.

It's a way to queue custom inline closures from thread to main (or another thread if you make support for it).  The way it captures expressions and values to synchronize is not air tight.

Some people advocate it as the magic bullet for threading, but IMHO it is severely overvalued. (and I work daily in Delphi XE3, so that opinion has nothing to do with FPC not supporting it)

As far as Mitov goes, I believe in long term support. Temperamental updating of the minimal version is not something I like in a component vendor.
« Last Edit: March 13, 2014, 04:01:20 pm by marcov »

Fiji

  • Guest
Re: Anonymous Methods
« Reply #6 on: March 13, 2014, 04:02:19 pm »
Well its not just Mitov. Devart are waiting for 2.7.3 or whatever version to support their EntityDAC. Not to mention FireDAC withdrawing their support for FPC..  :-\
 
« Last Edit: March 13, 2014, 04:24:23 pm by Fiji »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12630
  • FPC developer.
Re: Anonymous Methods
« Reply #7 on: March 13, 2014, 04:47:04 pm »
Well its not just Mitov. Devart are waiting for 2.7.3 or whatever version to support their EntityDAC.

They are welcome to contribute support. I think at best a beta for 2.8 will arrive in 2013. I doubt the final release will.

And target is delivering what is now in trunk, not providing a certain delphi level.

Quote
Not to mention FireDAC withdrawing their support for FPC..  :-\

We expected something more from the owner of FireDAC for years already after we promoted their platforms and provided a second source for close to twenty years :-)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Anonymous Methods
« Reply #8 on: March 13, 2014, 06:45:13 pm »
He dumped support for lazarous? When was that? This feature is in Delphi since 2009. It makes multi-threading very very easy among other things..

Here is a webinar from Mitov:

https://www.youtube.com/watch?v=7g-dmfSH6ZU

Ok I have seen the 1st 20 minutes or so and so far it seems as a time waster or to put it politely it goes out of his way just to find a problem to the anonymous methods solution. The only thing I got as a plus (which is quite good) is the ability to attach either procedures or methods to a "reference of procedure" instead of the more strict way to accept either methods or procedures but not both. Now that I have said that, there is a way around this as well using overloaded versions of the forEach method and some minimal code. As for the declaration of a variable and making it non thread safe BS is just a failure of imagination from mr Mitovs part. For example declare the procedure type with 2 parameters eg
Code: [Select]
tproc<T1, T2> = procedure (Item : T1; Userdata:T2) and there you have it, no memory waste or unsafe data access.

Should I keep viewing does it get better down the road?

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Fiji

  • Guest
Re: Anonymous Methods
« Reply #9 on: March 13, 2014, 07:05:58 pm »
Sure but can you do this with FPC? Yea I thought so. C# and C++11 both have it. I guess in 2-3 years we will see this, time will tell. But by that time Embarcadero will already be out with a Linux platform. Linux Server scheduled for 2015.  8-)

Code: [Select]
Parallel.ForEach(1, testSize).Execute(
  procedure (const elem: integer)
  begin
    // do something with 'elem'
  end);
« Last Edit: March 13, 2014, 07:11:27 pm by Fiji »

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Anonymous Methods
« Reply #10 on: March 13, 2014, 07:22:37 pm »
What if I store a pointer to anonymous method which accesses a local variable, then this variable goes out of scope? In C# and Java this variable will be preserved because anonymous function (or class in case of Java) still contain reference to this variable, but what happens in Delphi & FPC? This I do not know. Access Violation most likely. C++? Access violation as well, I guess
I personally do not think that anonymous methods is a must-have feature for FPC. What can be accomplished with anonymous methods can be easily accomplished in FPC without anonymous methods as well; saving about 10 lines of code is not a priority
« Last Edit: March 13, 2014, 07:25:32 pm by hinst »
Too late to escape fate

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Anonymous Methods
« Reply #11 on: March 13, 2014, 07:23:35 pm »
Sure but can you do this with FPC? Yea I thought so. C# and C++11 both have it. I guess in 2-3 years we will see this, time will tell. But by that time Embarcadero will already be out with a Linux platform. Linux Server scheduled for 2015.  8-)

Code: [Select]
Parallel.ForEach(1, testSize).Execute(
  procedure (const elem: integer)
  begin
    // do something with 'elem'
  end);
Yeah that is the ugly thing I'm trying to avoid for the same reason I don't like in line variable declaration that other languages have. What is the purpose of an anonymous method why would I need it to be anonymous? I prefer a named one that can be used when ever I feel like it and there is no need to retype the code again one procedure down or even on the next unit.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Anonymous Methods
« Reply #12 on: March 13, 2014, 07:52:07 pm »
It's not about your personal preferences or technical needs. It's all about marketing (minimum standards to use "modern language" brand in advertising :) ).

New developers will come (with experience of languages with closures) and they'll use it with Delphi/FPC. Eventually forcing everyone else (good-old-experienced developers) to use it (just because you might have to debug the code or work with component that's using it).

Sad, but true.

Using closures in  "threads" is questionable.

This is how I'd do it today:
Code: [Select]
type
  TDataForThread = class(TObject)
    // put some data here
  end;

procedure InitThread;
var
  d : TDataForThread;
begin
  d:=TDataForThread.Create;
  d.Gathet();
  StartThread( @ProcThread, d);
end;

procedure ProcThread(data: Pointer);
var
  d : TDataForThread;
begin
  d:=TDataForThread(data);

  // do the work

  d.Free;  // or not d.Free, depending on how you use it.
end;
Benefit - I've full control on how and when the memory is allocated / copied and released.

If I'd do it using closures (anonymous functions), it might look like this:  (which is less typing, but looks ugly):
Code: [Select]
..
  StartThread( procedure
    begin
      // do the work     
    end)
..
I'm loosing control over how/when and what is being released / allocated. Which makes me think about unnecessary growing memory consumption and possible AVs (as mentioned earlier in this thread).
« Last Edit: March 13, 2014, 07:58:36 pm by skalogryz »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12630
  • FPC developer.
Re: Anonymous Methods
« Reply #13 on: March 13, 2014, 08:04:36 pm »
Sure but can you do this with FPC? Yea I thought so. C# and C++11 both have it. I guess in 2-3 years we will see this, time will tell. But by that time Embarcadero will already be out with a Linux platform. Linux Server scheduled for 2015.  8-)

(and by then the cheapest version will probably be Eur 2500, at the rate it is currently going. Not strange, sb has to fund all this).

Quote
Code: [Select]
Parallel.ForEach(1, testSize).Execute(
  procedure (const elem: integer)
  begin
    // do something with 'elem'
  end);

Parallel for constructs are hardly useful, since only few real life programs are bound by a single for loop without side conditions (and more importantly without the need to WARN about side conditions, since these constructs are pure library and thus not guarded in any way)


avra

  • Hero Member
  • *****
  • Posts: 2582
    • Additional info
Re: Anonymous Methods
« Reply #14 on: March 14, 2014, 09:22:12 am »
How to use this with the latest build of FPC? This would make use of Omni Thread Library possible.
... and probably OpenWire. Mitov dumped Lazarus support because of latest Delphi features (XE2+) not available in FPC.

He dumped support for lazarus? When was that?

Last version supporting Lazarus was 5.0.3 from 2012. In 6.0.0 he says:
Quote
Support for Lazarus and Delphi versions older than XE2 is discontinued due to the new language features

I would not be so hostile to his decision. On the contrary. It would be interesting to hear from him self the list of missing language features that decided his move, no matter if FPC ever gets them or not. Initial Laz/FPC support was his own effort and he had to drop his own wasted hours to the trashcan, and that is not an easy decision for anyone. According to his CodeRage webinar, it seams that he likes new Delphi language features a lot. So much that he has decided to rewrite all his software because of them. OpenWire was just one of his many libraries being rewritten, and users are not forced to change the way they used it. Old code still works. Who knows, maybe without these new language features even Delphi support would be dropped? We should remember that his libraries are also targeting Visual Studio (where is the main cash flow nowadays for component writers), so I suspect that unification may be his motivation since new language features require different way of thinking.

I am not saying that Delphi should be followed at all costs and that we should all adapt new languages features when/if they are implemented, but I would hate to see another wall separating Delphi and Lazarus developers and not being able to use some good libraries just because of that.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018