Late binding for lamba parameters?

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


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




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