Fun with method references
Fredrik Ohrstrom
oehrstroem at gmail.com
Sun Aug 8 02:13:13 PDT 2010
2010/8/7 Rémi Forax <forax at univ-mlv.fr>:
>
> interface Handler<A> {
> public int handle(A a);
> }
>
> void foo(Handler<String> stringHandler) { ... }
> void foo(Handler<Bar> barHandler) { ... }
>
> which exhibits exactly the same erasure problem.
>
Exactly. I strongly believe that we need function types.
Whatever restrictions that are put upon them due to
erasure, will be lifted whenever we get erasure.
It will still be better than forcing a whole new set of
SAM-interfaces onto the Java world.
Also, for those who really want to have a sort of overloaded
functionality depending on the exact type of the function type
can do this:
void foo(Function<?> f) {
if (f invokeableby (String)->int) { fooString(f); } else {
if (f invokeableby (Bar)->int) { fooBar(f); }
}
If you do foo( (String s) -> s.length() ) the foo invokeableby checks
will be inlined and the if-statements dead-branch removed and you
get a direct call to fooString (which of course might be inlined as well.)
//Fredrik
More information about the lambda-dev
mailing list