Recent

Author Topic: Anonymous Methods  (Read 40342 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Anonymous Methods
« Reply #15 on: March 18, 2014, 10:09:44 pm »

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

But suddenly he starts making appearances on Embarcadero sponsored webinars ? Call me a cynic, but....

Not that I think that Embarcadero set him up against Lazarus. They have much bigger competition, namely old Delphi versions :)_)

Fiji

  • Guest
Re: Anonymous Methods
« Reply #16 on: July 10, 2014, 10:44:58 am »
I did some benchmarking, anonymous methods are a lot slower because they use interfaces. Using a callback is much much faster..
So now when I think about it, anonymous method would make absolutely no sense what so ever.

Code: [Select]
TCompare = function(const L, R: Integer): Integer;
« Last Edit: July 10, 2014, 10:47:18 am by Fiji »

mic

  • Newbie
  • Posts: 2
Re: Anonymous Methods
« Reply #17 on: July 26, 2014, 02:46:07 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.
Hi. I am interested in using Lazarus for some projects but this turns me off. I cannot believe that in 2014 somebody needs to have the benefits of anonymous methods explained to them.
There are any number of articles on the topic of treating functions as data. Maybe try http://en.wikipedia.org/wiki/Higher-order_function
A full implementation would involve supporting closures, which means capturing local variables with a code reference so they don't expire along with the local stack. http://en.wikipedia.org/wiki/Closure_%28computer_programming%29
I understand that they are fairly expensive to implement, but I submit that  arguing that it is not needed because it can be done a another is like arguing repeat/while/for is not needed because we have labels and goto.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Anonymous Methods
« Reply #18 on: July 26, 2014, 04:01:34 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.
Hi. I am interested in using Lazarus for some projects but this turns me off. I cannot believe that in 2014 somebody needs to have the benefits of anonymous methods explained to them.

I'm sorry I'm not as enlightened as you are. I'll do my best to catch up. until then I have no use for anonymous methods sorry.

There are any number of articles on the topic of treating functions as data. Maybe try http://en.wikipedia.org/wiki/Higher-order_function
A full implementation would involve supporting closures, which means capturing local variables with a code reference so they don't expire along with the local stack. http://en.wikipedia.org/wiki/Closure_%28computer_programming%29

Thanks for the links I'll take a look.

I understand that they are fairly expensive to implement, but I submit that  arguing that it is not needed because it can be done a another is like arguing repeat/while/for is not needed because we have labels and goto.

So do you have anything of value to share other than cheap shots and exaggerated metaphors that is?
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

mic

  • Newbie
  • Posts: 2
Re: Anonymous Methods
« Reply #19 on: July 26, 2014, 07:09:09 pm »
That's great. I'm happy if I have provoked your curiosity. I think closures and anonymous methods are logical extension of  support for delegation in the language. I know in many ways it's just syntactic sugar but the point I was trying to make with my cheap shot is that syntactic sugar can be very sweet!

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Anonymous Methods
« Reply #20 on: July 27, 2014, 07:31:54 am »
improve lazarus copying delphi is not the best way in my opinion.

Anonymous methods are inducements to java developers (who don't know how use pointers) adhere to embarcadero's suite.

Anonymous methods are new features to a new commercial product, not a real need.

if anyone look for a feature to convert Delphi code to FPC code i believe this can be a real need, but make unnecessary feature in in FPC because some (java*) developers don't know use pointers in function parameters is a litle hard to accept.

http://wiki.freepascal.org/How_to_use_procedural_variables

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Anonymous Methods
« Reply #21 on: July 27, 2014, 07:53:10 am »
improve lazarus copying delphi is not the best way in my opinion.

Anonymous methods are inducements to java developers (who don't know how use pointers) adhere to embarcadero's suite.

Anonymous methods are new features to a new commercial product, not a real need.

if anyone look for a feature to convert Delphi code to FPC code i believe this can be a real need, but make unnecessary feature in in FPC because some (java*) developers don't know use pointers in function parameters is a litle hard to accept.

http://wiki.freepascal.org/How_to_use_procedural_variables

Well even the wiki article on higher order functions lists the function pointers as an alternative but the wiki article on closures raises a valid question about the local variables and when they are get released. Although this becomes a problem only when a local procedure or function is called from a secondary thread. I'm trying to see if there is a problem with asynchronous components /applications but I can't seem to find a good example to look at so for now I would say that in 90% of the cases a closure is not needed with 4% being multithreading and the rest something I missed.
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

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anonymous Methods
« Reply #22 on: July 27, 2014, 10:45:53 am »
improve lazarus copying delphi is not the best way in my opinion.

Anonymous methods are inducements to java developers (who don't know how use pointers) adhere to embarcadero's suite.

Anonymous methods are new features to a new commercial product, not a real need.

if anyone look for a feature to convert Delphi code to FPC code i believe this can be a real need, but make unnecessary feature in in FPC because some (java*) developers don't know use pointers in function parameters is a litle hard to accept.

http://wiki.freepascal.org/How_to_use_procedural_variables
http://en.wikipedia.org/wiki/Anonymous_function
Actually, it's been in Lisp since 1958, Java only implements it. Anyway, Java introduces anonymous method quite lately in Java 7, previously they only have anonymous inner class, which is similar but different concept. But yes, other than syntactically lighter (common human words: less typing) for certain cases, it doesn't seem to have any other significance in imperative languages. The story would be different in functional languages, though.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Anonymous Methods
« Reply #23 on: July 27, 2014, 12:12:39 pm »
The wikipedia page about closures says:
---
The term closure is often mistakenly used to mean anonymous function. This is probably because many programmers learn about both concepts at the same time, in the form of small helper functions that are anonymous closures. An anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the lexical environment section below).
---
It makes me confused because I actually thought they were the same thing.
Does it mean an anonymous function can be a closure but not necessarily?
Or does it mean an anonymous function is always a closure but a closure is not always an anonymous function?
Uhhh...
Syntactic sugar that reduces code is good but the syntax should also be intuitive and simple. I also don't think Delphi's syntax for this feature is good. It conflicts with the rest of Pascal.

We have a warning example about syntax gone out of hands, namely C++.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anonymous Methods
« Reply #24 on: July 27, 2014, 12:31:33 pm »
It makes me confused because I actually thought they were the same thing.
Does it mean an anonymous function can be a closure but not necessarily?
Or does it mean an anonymous function is always a closure but a closure is not always an anonymous function?
Uhhh...
I was about to write, when I find someone else already wrote a good simple explanation: http://stackoverflow.com/a/4912176/549472

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Anonymous Methods
« Reply #25 on: July 27, 2014, 04:15:07 pm »
I was about to write, when I find someone else already wrote a good simple explanation: http://stackoverflow.com/a/4912176/549472

Thanks.
It seems the terms are generally used in a wrong way. Benefits of Delphi's anonymous functions are praised while actually they are benefits of closures.
Maybe Embarcadero itself is confused by the terms.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Anonymous Methods
« Reply #26 on: July 27, 2014, 05:00:44 pm »
Does it mean an anonymous function can be a closure but not necessarily?

Afaik: Passing on an anonymous function to something of type "reference to <function definition>" is the actual closure, not the anonymous method itself.  (e.g. TThread.Queue() and synchronize)

Since then the parameters must be saved till it is actually time to execute the function. These are captured in a interface like refcounted memory section.

Note that while anonymous functions thus have some closure-like traits, don't go overboard with the functional programming analogies. Anonymous functions are mainly meant to easily run small fragments of code in other threads, not to provide a functional programming dimension to Pascal.

For that it is good, but IMHO people read much more into it then there is really there.

Fiji

  • Guest
Re: Anonymous Methods
« Reply #27 on: July 27, 2014, 10:57:49 pm »
Even C++ has lambdas. I think this should be a wake up call for FPC. And yes JAVA has it too.  8-)

I thought it was already implemented by some "secret" developer in the trunk.  I think FPC needs to be compatible fully or atleast fully to Delphi.
Majority of the libraries are for Delphi. Infact there are TONS of them. Why don't they get ported? Compatibility.
« Last Edit: July 27, 2014, 11:00:38 pm by Fiji »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Anonymous Methods
« Reply #28 on: July 28, 2014, 04:10:22 am »
Even C++ has lambdas. I think this should be a wake up call for FPC. And yes JAVA has it too.  8-)

I thought it was already implemented by some "secret" developer in the trunk.  I think FPC needs to be compatible fully or atleast fully to Delphi.
Branches are no secret, they're just sandboxes to keep the feature from breaking the main branch development, which most people use. In case you haven't known, the principle for the core developers is to implement what could give significant effect, and not all Delphi features are eligible. However, if somebody is willing to implement, maintain and ensure it doesn't break existing code, then it's fine. As with helper types, we even have the more advanced one than Delphi. More to read here and here
Majority of the libraries are for Delphi. Infact there are TONS of them. Why don't they get ported? Compatibility.
Not always, mostly due to JEDI or Windows dependency. A properly designed one could easily get ported, or even directly compilable. Not many Delphi libraries use anonymous function, I believe.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Anonymous Methods
« Reply #29 on: July 28, 2014, 08:50:16 am »
Many years ago I bought D2009 which has built in anonymous methods. But I have done nothing with them. I find them difficult to use and with my code I can do anything I want (for now).
Why should Lazarus be the same as Delphi? If it should be, Lazarus is only interesting for people who can't by Delphi (except the *unix users). I like to use a application that offers me extended functionality then other develop application. If it's available, it's good to have.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018