Late binding for lamba parameters?
Remi Forax
forax at univ-mlv.fr
Fri Sep 13 03:37:46 PDT 2013
On 09/13/2013 11:41 AM, Millies, Sebastian 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?
No, it was not discussed at all.
Java is a single dispatch language not a multi-dispatch one.
You can take a look to languages like Cecil, Kea, Dylan or Groovy
if you're looking for a language that do multi-dispatch.
Introducing multi-dispatch in a typesafe language is something hard.
By example, using your example above, what if one of the element of the list
is an AbstractElem at runtime, or worst what if the element is null.
>
>
> 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
>
>
Rémi Forax
More information about the lambda-dev
mailing list