views:

199

answers:

3

Is my interpretation of lambda expression in the context of c++ and Java is correct?

+1  A: 

C++0x lambda expressions are unnamed methods, Javas anonymous classes are unnamed classes. So they share not having a name but the concepts are different.

Starting with the most obvious fact, that lambda functions (may) return a value and anonymous classes can be used to create instances (objects).

BTW - wikipedia mentions, that only lambda functions are proposed for C++0x.

Andreas_D
using the word method is a bit miss leading, lambda expressions are not anonymous methods they are anonymous function.
snk_kid
@snk_kid: In C++, they are the same thing. Class functions are member methods.
DeadMG
@DeadMG Instance methods are not the same as a function, especially in the traditional/original meaning of the word from languages such as Smalltalk and Objective-C. Methods are OO terminology, they are not lambda calculus or functional programming terminology, there are no 'methods' in functional languages such as Haskell or SML.So as I said using the word method is misleading and incorrect.
snk_kid
It all depends on whether there's a context object, a `this`. Methods have one, functions don't. (Of course, it might also be syntactically hidden which would make the difference rather moot.)
Donal Fellows
@snk_kid: Who cares about those other languages? This is C++. A member function is a member method. A regular function is a regular method. They are the same thing.
DeadMG
@Donal Fellows: lambda functions are functions in the mathematical sense from lambda caluclus, lambda caluclus does no the use the terminology method. The original definition of a method are not much like a mathematical functions but message passing. In functional languages which support (lexical) closures they do not usually have a 'this' variable (even if they implement a closure this way).@DeadMG: Who cares? where do you think these ideas come from? they are not made up they have formal definitions. You're incorrect and this is your only answer because you know you are wrong.
snk_kid
@DeadMG. No, they're not. A method is very explicitly a function called on an object, i.e. a member function. There is no such thing as a "regular method". Besides, in C++ member "methods" are called member functions. C++ does not use the term "method".
Peter Alexander
@snk_kid: I thought we were talking about C++. If you're talking about some other language then all bets are off and I suspect we won't have a very useful conversation.
Donal Fellows
The discussion is moot. The standard does not talk at all about methods, only about functions --free standing functions, member functions, static class functions--. The term has been borrowed from other languages with OO and in most context refers to either *member functions* or *static class functions*. On the other hand, lambdas do not create free standing functions, but rather *function objects* with a single `operator()` method. I don't think that *method* is the best choice of term and neither would be *function*, but however you call them, I will sleep as tight tonight...
David Rodríguez - dribeas
@Donal Fellows I'm not talking about some other language, I'm talking about lambda functions their fundamental basis and terminology not implementation details. Lambda calculus is not a particular programming language. if you want to talk about C++ specifically it's called lambda function, and there is no usage of the word unnamed method in the ISO standard.
snk_kid
@David Rodríguez the fact that C++0x lambda functions LOGICALLY generates a (local) class with the functional call operator overloaded is just an implementation detail. The correct term is lambda function/expression and it's called that for good reason. I said logically because a compiler does not actually have to implement it that way.
snk_kid
@snk_kid: I won't discuss this. From the C++0x Final Draft (n3092) §5.1.2[expr.prim.lambda]/1: '*Lambda expressions provide a concise way to create simple function objects.*'. I am not the person to discuss the term with, get in touch with any of the committee members. I already said that the discussion is moot.
David Rodríguez - dribeas
David Rodríguez - dribeas@ You said "function objects with a single operator() method" the extra part is just an implementation detail. Function object doesn't mean it's not a function still, first-class functions are function values. They are still functions if they are treated as a value or not.
snk_kid
+3  A: 

They are not quite as same. Both create unnamed classes, but their similarity ends at that point.

In C++ you create a closure catching your local variables, optionally by reference. In Java, you just get a snapshot of the current local variable's values (and those variables need to be "final").

The purpose of anonymous inner classes is to extend another class or implement another interface ad-hoc. For that reason, anonymous inner classes can simulate the job of lambda expressions to some extend, for example by implementing the Runnable interface. Lambda expressions are specifically designed to be called and possibly modify the local variables in their environment.

Johannes Schaub - litb
Except accessing the variables in scope there is no difference right?
yesraaj
@yesraaj, i don't know of the very details of inner classes. But since the purpose is entirely different, i think we can't compare them using sane methods. In anonymous inner classes, what you specify in the body is copied into the unnamed class body that's generated. So you may add multiple methods, class members, etc.. For c++ lambdas, you just specify the content of the "operator()".
Johannes Schaub - litb
+1. As a side note, C# provides yet another different approach to the problem of closures with its own lambdas that always capture by reference all local variables. You can read about Java/C# in [The Beauty of Closures](http://csharpindepth.com/Articles/Chapter5/Closures.aspx) in Jon Skeet's blog. The article does not deal with c++ lambdas, where you can either capture by value or by reference, but as in most other cases in c++, you must beware of the lifetime of your captured references --the lambda should not be used after the referenced objects have been destroyed.
David Rodríguez - dribeas
+2  A: 

A Java anonymous inner class can refer to final data in the enclosing method, and to all data (including mutable) in the enclosing class. So methods in anonymous classes cannot change the values of variables in the enclosing method, but they can change values of members in the enclosing class.

A C++ lambda can refer to all data (including mutable) in the enclosing function, and if it is nested inside a member function then it can do the same for the data of the enclosing class. The precise degree of dependency on the enclosing scope(s) is declared by the programmer, so it is explicit rather than implicit.

This makes them quite similar but the Java feature treats local variables/parameters in methods differently, on the principle that they should not be mutable from outside the method, especially in a language which has traditionally employed threading so casually.

Compare with C# lambdas, which have no restrictions and all dependencies are implicit. This makes them by far the least verbose of these features (also helped by them having the best type inference). But on the downside, they invalidate all the simple rules about threading, i.e. it is no longer necessarily true that local variables are "on the thread stack" and hence never require locking before access.

Daniel Earwicker