Method references with types [Re: lambda syntax tutorial]
Collin Fagan
collin.fagan at gmail.com
Thu Aug 5 19:28:56 PDT 2010
I did some experimenting with the different literals from FCM and I have say
it removed unbelievable amounts of boilerplate code.
http://java.dzone.com/news/experimenting-fcm
Looking back I have to say one thing I didn't emphasize enough was how
powerful this is when combined with generics. Basically one you write a
ListModel that takes a method reference to you pretty much never have to
write another one again. Once you build a TableModel that defines columns
via methods refrences you never need to do it again.
Here is an example:
MethodRefrenceTableModel<Person> personModel = new
MethodRefrenceTableModel<>(); // haha diamond operator!
model.addColumn("First Name", Person#getFirstName());
model.addColumn("Last Name", Person#getLastName());
model.addColumn("Date Of Birth", Person#getDob());
MethodRefrenceTableModel<Planets> planetModel = new
MethodRefrenceTableModel<>();
model.addColumn("Name", Planet#getName());
model.addColumn("Size", Planet#getSize());
Now these are all Swing examples but the same idea applies to Apache Wicket
or basically any other view technology.
How about a comparator?
Comparator<Person> firstNameFirst = new Comparator( Person#getFirstName(),
Person#getLastName());
Comparator<Person> lastNameFirst = new Comparator( Person#getLastName(),
Person#getFirstName());
JPA 2.0 generates a Metamodel which is very similar to a method reference.
(OK two method references which make it more like a property.)
http://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/
It might look more like this with method references.
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Person.class);
Root person = criteriaQuery.from(Person.class);
criteriaQuery.select(person).where(criteriaBuilder.equal(Person#getFirstName()),
"Bob");
I know these examples are probably a bit blue collar but if it doesn't push
too far outside the scope of this project I'd like to see thease type of use
cases considered.
Thanks,
Collin
On Thu, Aug 5, 2010 at 6:09 PM, Stephen Colebourne <scolebourne at joda.org>wrote:
> On 5 August 2010 22:08, Brian Goetz <brian.goetz at oracle.com> wrote:
> > We are for this reason considering #foo.bar() instead of #foo.bar. We
> are
> > also considering a greedy prefix syntax instead of infix.
>
> I'm assuming these two sentences are independent, as I can't see why
> changing the syntax solves the problem (well, I can, but only in the
> sense of having method and field references look very different, which
> seems daft).
>
> As discussed in a previous thread, John, Josh and I will steer you
> strongly towards infix. The current prototype has got it right.
>
> I'd also note that Foo#bar(...) is acceptable to me, although it feels
> somewhat pointless.
>
> Stephen
>
>
More information about the lambda-dev
mailing list