Late binding for lamba parameters?

Zhong Yu zhong.j.yu at gmail.com
Fri Sep 13 11:52:30 PDT 2013


On Fri, Sep 13, 2013 at 1:20 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> On 09/13/2013 08:10 PM, Zhong Yu wrote:
>> We could build a run time dispatcher, for example
>>
>> public class Dispatcher
>>      <T> void register(Class<T> type, Consumer<T> handler)...
>>      void call(Object obj)...
>>
>> ---
>>
>> Dispatcher processor = new Dispatcher();
>>
>> processor.register(Elem1.class, elem1->{
>>      print(elem1.f1);
>> });
>>
>> processor.register(Elem2.class, elem2->{
>>      print(elem2.f2);
>> };
>>
>> list.forEach(processor::call);
>>
>> ---
>>
>> the syntax of registration is pretty close to method declaration syntax.
>>
>> The dispatcher requires some effort to mimic static method invocation
>> semantics, e.g. applicable through subtyping; selecting the most
>> specific method. But it is far easier since the dispatcher
>> doesn't/cannot care about generic stuff.
>>
>> Zhong Yu
>
> and shameless plug, here is the way to implement it:
> http://www.jot.fm/issues/issue_2005_12/article3/index.html

That is cool and comprehensive.

Regarding the syntax of declaring double-dispatched methods, the
convention based syntax

    B visit(A)

does not get much static checking; mistakes can be made on method
name, input/output types.

The lambda approach

    visitor.register(A.class, a->{... return (B)something; })

can get all 3 parts checked statically.

With lambda at hand, I'm sensing that some old practices based on
convention/annotation/overriding will be replaced by explicitly
injecting lambda.


>
> cheers,
> Rémi
>
>>
>>
>>
>>
>> On Fri, Sep 13, 2013 at 4:41 AM, Millies, Sebastian
>> <Sebastian.Millies at softwareag.com> wrote:
>>> Hello there,
>>>
>>> as Project Lambda has introduced some elements of laziness into Java, I
>>> wondered about the following question:
>>> Is it possible to have methods in a lambda body dispatch on an argument type
>>> determined at run time? Or in otherwords, can the type inference for the
>>> lambda parameter be delayed by using some sort of type variable?
>>>
>>> Example:
>>>
>>> class AbstractElem {}
>>> class ConcreteElem1 extends AbstractElem {}
>>> class ConcreteElem2 extends AbstractElem {}
>>>
>>> void process() {
>>>    List<? extends AbstractElem> list = new ArrayList<>();
>>>    list.forEach( e -> processConcreteElem(e) );
>>> }
>>>
>>> void processConcreteElem(ConcreteElem1 e) {...}
>>> void processConcreteElem(ConcreteElem2 e) {...}
>>>
>>> This would be really useful, not having to do double-dispatch through the
>>> Visitor pattern, but it seems it's still impossible, right? Has this been discussed
>>> at all?
>>>
>>>
>>> n  Sebastian
>>>
>>> Software AG - Sitz/Registered office: Uhlandstra?e 12, 64297 Darmstadt, Germany - Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
>>>
>>>
>
>


More information about the lambda-dev mailing list