On Jun 17, 2021, at 4:40 AM, Remi Forax <fo...@univ-mlv.fr<mailto:fo...@univ-mlv.fr>> wrote:
As a stretch move, I think we can even retro-upgrade the type checking of Objects::newIdentity with type variable trickery, when IdentityObject becomes real. Please see: https://bugs.openjdk.java.net/secure/attachment/95170/Foo.java https://bugs.openjdk.java.net/browse/JDK-8268919 I wonder if a simple way to avoid to allow any Ts if to allow to specify an intersection type as parameter type and/or return type of methods (we have 'var' inside the body) so instead of public static <T extends Object & IdentityObject> T newIdentity() { we can write public static Object & IdentityObject newIdentity() { This requires a grammar change but it's exactly the type we want. Yes. I was trying to avoid that. My attempt (see above) is wrong because (as Dan points out) it can infer T to be String, which is false. The type variable T would need lower bound of Identity, as well as an upper bound (and erasure) of Object. But, as with return type intersections, there is no syntax to express that, apparently. Only wildcards can have lower bounds, right? And you can’t have a (lower-bounded) wildcard for a return type: public static List<? super IdentityObject> newIdentityList(); //OK public static ? super IdentityObject newIdentity(); //not OK