Method references with types [Re: lambda syntax tutorial]
David Goodenough
david.goodenough at linkchoose.co.uk
Sun Aug 8 02:42:15 PDT 2010
On Sunday 08 August 2010, Fredrik Ohrstrom wrote:
> 2010/8/5 Stephen Colebourne <scolebourne at joda.org>:
> > No one has yet mentioned another danger with using Foo#bar instead
of
> > Foo#bar(String).
> >
> > The former choice at this point closes off options in the future for
> > Java.
> >
> > Foo#bar is the only sensible syntax for a field literal. Java can have
> > both fields and methods with the same name. This has the potential
to
> > be a limitation to future language development.
>
> JSR292 will never offer field literals. However JSR292 offers a quick
> creation of getters and setter methods. These are pure methodhandles,
ie.
> code.
>
> The use cases for pure field literals are a magnitude fewer than for
> method references literals.
I suspect that this is because most of them are hidden. Look at all
the places where the name of a field is passed as a String and all of
these are places where field literals would be applicable. I am here
thinking of Bean Properties, JDBC and all the JPAs, Swing and the many
binding frameworks.
Whether these qualify as "pure field literals" I leave for others to judge,
but the need for field litereals is real and pressing.
Mostly Java is a language that the compiler and IDEs can check, but as
soon as field names have to be passed around as names neither the
compiler nor IDEs can help.
Now I suspect that the fact the compiler and JVM use names all over the
place obscures the problem, but the difference here is that the names
are only used after they have been regiourously checked.
David
>
> If you want field setters and getters it is trivial to write:
> Function(Person)->int getage = { p -> p.age };
>
> Then Javac can detect that this is an "age getter" and use
> MethodHandles.lookup to acquire a field getter.
>
> When method reference literals are supported, 9999 programmers will
write
> save_button.onClick(this#save);
> every day, and one programmer will write code for a field literal.
>
> //Fredrik
More information about the lambda-dev
mailing list