Method calls vs lambda calls
Neal Gafter
neal at gafter.com
Thu Dec 17 00:09:02 PST 2009
On Thu, Dec 17, 2009 at 12:02 AM, Howard Lovatt <howard.lovatt at iee.org>wrote:
> Based on Neal's generic example, here is a particularly nasty case:
>
> class Base<T> {
> T t;
> T t1;
> void t() { out.println( t ); }
> }
>
> class Derived extends Base<#int()> {
> {
> t = #int() (42); // Can't override t() because it has a different
> signature
> t1 = #int() (41);
> }
> void test() {
> out.println( t1 ); // Prints Function_intATXXX
> out.println( t1() ); // Would be nice if this printed 41
> out.println( t ); // Prints Function_intATXXX
> out.println( t() ); // Error t() returns void
> }
> }
> ...
> Can anyone think of any other options?
>
Yes: a variable is added to the method namespace only when declared with a
function type. In this example, t and t1 are not declared with a function
type, but rather are declared with the type of a type parameter (T).
Therefore shorthand invocation is not available for them.
This is a nice solution because it prevents names being inherited into a
namespace in which they did not previously appear.
Cheers,
Neal
More information about the lambda-dev
mailing list