More type mystery
Marc Petit-Huguenin
marc at petit-huguenin.org
Tue Feb 11 14:13:22 PST 2014
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Thanks Paul, that's really helpful.
On 02/11/2014 08:52 AM, Paul Govereau wrote:
> Hi Marc,
>
> Java's type inference isn't always able to fill in missing types. Sometimes
> the programmer must supply some types manually as hints to type inference.
> Unfortunately, your program is one of these cases. Technically, this error
> stems from the fact that the method invocation of apply is not a "poly
> expression" as described in section 15.12 of the Java Language
> Specification version 1.8. This means type inference will not propagate
> type constraints through this expression, and therefore is unable to figure
> out how to instantiate the parameter T of IS_ZERO.
>
> As you discovered, you can supply type information by using a variable for
> part of your expression with the correct type. Another way is to manually
> supply the type parameter for the generic method using this syntax:
>
> Test.<Boolean>IS_ZERO()
>
> Your last example provides the necessary type information by adding an
> argument that requires (T=Boolean) for the generic method to be called.
>
> My understanding is that this particular constraint may be able to be
> relaxed. However, more work is needed to verify this before any changes can
> be made to Java's type inference.
>
> Paul
>
> On 02/08/2014 09:35 AM, Marc Petit-Huguenin wrote: With these static
> methods:
>
> static<T> UnaryOperator<UnaryOperator<T>> ZERO() { return p -> x -> x; }
> static<T> UnaryOperator<UnaryOperator<T>> ONE() { return p -> x ->
> p.apply(x); } static<T> Function<T,UnaryOperator<T>> TRUE() { return x -> y
> -> x; } static<T> Function<T,UnaryOperator<T>> FALSE() { return x -> y ->
> y; } static<T>
> Function<UnaryOperator<UnaryOperator<Function<T,UnaryOperator<T>>>>,Function<T,UnaryOperator<T>>>
>
>
IS_ZERO() { return n -> n.apply(x -> FALSE()).apply(TRUE()); }
> static<T>
> Function<UnaryOperator<UnaryOperator<Function<T,UnaryOperator<T>>>>,Function<T,UnaryOperator<T>>>
>
>
IS_ZERO(Class<T> t) { return n -> n.apply(x ->
> FALSE()).apply(TRUE()); } static boolean
> toBoolean(Function<Boolean,UnaryOperator<Boolean>> f) { return
> f.apply(true).apply(false); }
>
>
> Why is this not compiling:
>
> System.out.println(toBoolean(IS_ZERO().apply(ZERO())));
> System.out.println(toBoolean(IS_ZERO().apply(ONE())));
>
> Test.java:18: error: method toBoolean in class Test cannot be applied to
> given types; System.out.println(toBoolean(IS_ZERO().apply(ZERO()))); ^
> required: Function<Boolean,UnaryOperator<Boolean>> found:
> Function<Object,UnaryOperator<Object>> reason: argument mismatch;
> Function<Object,UnaryOperator<Object>> cannot be converted to
> Function<Boolean,UnaryOperator<Boolean>> Test.java:19: error: method
> toBoolean in class Test cannot be applied to given types;
> System.out.println(toBoolean(IS_ZERO().apply(ONE()))); ^ required:
> Function<Boolean,UnaryOperator<Boolean>> found:
> Function<Object,UnaryOperator<Object>> reason: argument mismatch;
> Function<Object,UnaryOperator<Object>> cannot be converted to
> Function<Boolean,UnaryOperator<Boolean>> 2 errors
>
> But this works fine:
>
> Function<UnaryOperator<UnaryOperator<Function<Boolean,UnaryOperator<Boolean>>>>,Function<Boolean,
>
>
UnaryOperator<Boolean>>> IZ = IS_ZERO();
> System.out.println(toBoolean(IZ.apply(ZERO())));
> System.out.println(toBoolean(IZ.apply(ONE())));
>
> And this works fine:
>
> System.out.println(toBoolean(IS_ZERO(Boolean.class).apply(ZERO())));
> System.out.println(toBoolean(IS_ZERO(Boolean.class).apply(ONE())));
>
>
> Thanks.
>
>>
>
- --
Marc Petit-Huguenin
Email: marc at petit-huguenin.org
Blog: http://blog.marc.petit-huguenin.org
Profile: http://www.linkedin.com/in/petithug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQIcBAEBCAAGBQJS+qB/AAoJECnERZXWan7EvXoP/026d/C89yvImIyZB5TdOW5R
FfHC6meuPGp0QplmbwxtFahNYx8iUoZueGWYWniTjuuYAaNUZAckFVArrQQnGvTP
ldM+vXD0uP1u503whPKr+WoM3h2VbC4WBSLvjseNK9zOScJAW5+CyFnCHeL9l9bb
6M2TFD2Qn2Ca8A0W+SZuuJ+RRM/Dc5FhMOZtg4280YdGnk5D8cyOC9oHqadz06uj
aOHvhrRnudakp8RYmzAJxm5gxNL9fe7sYSMtEffrFs/S06jFfAmoBeFW+javxB2X
uqTpDoPtPC+leJq5Xsst5Eg/5AWe/oWllYnUKhiKOnfL87Gxe+YPAvN9yVsl/Nz6
5t5OVI5rZ6uh4M/hQ7AQQQSVdrnG3r2C/cm+QL0noRn4zv9jW/XNfeRu7Xx4KcBQ
gZ2k3Yj2FcUPv8PBUlE24naj8yoi7xplTWW8qKpPBJilV9gOxfwVWo19tZNSxe2/
D1FLjVOQ+4b7OILPC/I1iUCri++2YO77Pe730lOi5ksF/AX+ijsIWQAsXnD0n/u5
MujDUnhGQo7Sf/54H3pgOSoeoU8c0eR7oopcPHlPFnPfeiklH+/QWCkHzhyW3GyC
VwkqCpWGO6vG6EQH9XYuDig11PJP/BqqJfXkz/BF17BOtjNA67398UXugjaWy5Da
cNnvLAtw0nECTFZhzGKv
=KvfM
-----END PGP SIGNATURE-----
More information about the lambda-dev
mailing list