Call chains and method handles

John Nilsson john at milsson.nu
Sun Feb 28 07:37:49 PST 2010


It was suggested in some thread of conversation that method references was
going to be the most common use case for lambdas. I was thinking of whether
this was true for our code base (a 200kloc ERP web application with a
swing-like html-framework).

As the code is written now I don't think there would be much use for direct
method references. The closest thing is a report API that uses strings to
compile call chains against a collection of objects.

Report<Customer> r = report(Customer.class,"name","address.street")
...
searchButton.onClick(()->
r.show(customer.findByCustomerNo(customerNoField.getText()))

I would love to be able to replace those strings with statically verified
call chains. As it is now it's too fragile when refactoring.

So when Java7 comes we'll probably rewrite that to
Report<Customer> r = report(Customer.class,(Customer
c)->c.getName(),(Customer c)->c.getAddress().getStreet())

But now we've actually added boilerplate.

MethodRererences could maybe be used to make it

Report<Customer> r =
report(Customer.class,cc(Customer#getName),cc(Customer#getAddress,Address#getStreet))

Still lots of boilerplate, and cc wouldn't be type safe, which could be
changed by adding some more boiler of course: cc(...).andThen(...)

Any thoughts no how this could be improved? Should it be?

Ideally I would like to write something no more verbose than

Report<Customer> r = report(Customer.class,#getName,#getAddress#getStreet)

BR,
John


More information about the lambda-dev mailing list