About underscores in type arguments / partial type arguments
Red IO
redio.development at gmail.com
Sat Jan 21 10:40:01 UTC 2023
I'm not sure how well this would fit in Java, but rust has long reaching
backwards inference. Example:
var hashmap = new Hashmap<>();
hashmap.add(5, "Hello");
Where the first use of a type variable here int, String infers the type of
the hashmap.
This would allow patterns to avoid specifying the type variables they want
by inferring them by usage inside the pattern.
Great regards
RedIODev
On Sat, Jan 21, 2023, 08:01 Remi Forax <forax at univ-mlv.fr> wrote:
>
>
> ------------------------------
>
> *From: *"Tesla Ice Zhang" <ice1000kotlin at foxmail.com>
> *To: *"amber-dev" <amber-dev at openjdk.org>
> *Sent: *Saturday, January 21, 2023 3:23:39 AM
> *Subject: *About underscores in type arguments / partial type arguments
>
> Hi all,
> Is there any discussion related to partially specified type arguments
> (which are left for inference)? I just saw that Kotlin 1.7 added support
> for that. It would be nice if Java can have that too:
> https://kotlinlang.org/docs/whatsnew17.html#underscore-operator-for-type-arguments
>
> The demonstration of the feature in the above document is also applicable
> to Java.
>
>
> Yes, this has been discussed several times not only in the context of
> patterns but also in the context of inference in general.
>
> For patterns, currently you have to repeat all type arguments
>
> Map<String, Integer> map = ...
> switch(map) {
> case HashMap<String, Integer> m -> ...
> ...
>
> we talk about simplify it using
> case HashMap<> m -> ...
>
> but this means also supporting
> if (map instanceof HashMap<> m) { ...
>
> and maybe also supporting
> if (map instanceof HashMap) {
> var m = (HashMap<>) map;
> ...
> }
>
> One issue is that the inference rules are not the same as the usual
> inference rules, because you want to be able to infer something like
> HashMap<?,?> while you do not want to infer new HashMap<?,?>() in the
> general context.
>
> Currently, we are trying to see if we can avoid to specify the angle
> brackets all together, so more "case HashMap" than "case HashMap<>".
> see
> https://cr.openjdk.java.net/~gbierman/jep432%2b433/jep432+433-20221115/specs/patterns-switch-record-patterns-jls.html#jls-18.5.5
>
>
> Best regards,
> Tesla
>
>
> regards,
> Rémi
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230121/16d01a2b/attachment-0001.htm>
More information about the amber-dev
mailing list