Function.identity returns different type?

Zhong Yu zhong.j.yu at gmail.com
Wed Feb 26 11:53:16 PST 2014


When inferring

    (TargetType) expr.method(args)

expr must be evaluated first, independent of context.
TargetType/method/args imposes no constraint on expr whatsoever.

So in a chain method call

    (TargetType) a.m1().m2()

a.m1() is compiled as if it's an isolated expression,

    var x = a.m1();
    (TargetType) x.m2();

Zhong Yu


On Wed, Feb 26, 2014 at 4:14 AM, Timo Kinnunen <timo.kinnunen at gmail.com> wrote:
> Hi,
>
>
>
>
> What's the deal here, how come the "asIf" version - same as "asIn" except the method "asGiven" inlined away - doesn't compile?
>
>
>
>
>   static <T> T asIn(T t) {    return asGiven(Function.identity(), t);  }
>
>   static <T> T asIf(T t) {    return Function.identity().apply(t);  }
>
>   static <S, T> T asGiven(Function<S, T> given, S s) {    return given.apply(s);  }
>
>
>
>
> The compile error I get is:
>
> error: incompatible types: Object cannot be converted to T
>
>
>
>
> But there's no mention Object in the code...
>
>
>
>
>
> --
> Have a nice day,
> Timo.
>
> Sent from Windows Mail
>


More information about the lambda-dev mailing list