RFR: JDK-8235621: Replace use of Function<?,Integer> with ToIntFunction
Remi Forax
forax at univ-mlv.fr
Wed Dec 11 22:07:30 UTC 2019
Maurizio, Jon,
It's not always a trade off between simplicity and completeness,
you have other factors:
- code inspector of IDEs that will warn you, and because you have now too many warnings, you just de-activate the code inspector,
creating a vicious circle.
- types that come from type inference, so a user doesn't fully control the type sent as argument. From my experience, it's a common way to get Object instead of a subtype.
- teachers do not scale. As a teacher, you have to explain how wildcard works and then to explain why some random APIs of the JDK will not follow the rule. My answer is usually "ahhh, humans".
You're right that thanks to erasure you can always add the wildcard later.
And the right way(TM) to fix that issue if to introduce declaration site variance because use site annotations is not user friendly.
Anyway, as you say it's not important, it does not worth to spend more time on that.
So not introducing "? super" is fine for me. ahhh, humans :)
regards,
Rémi
----- Mail original -----
> De: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>
> À: "jonathan gibbons" <jonathan.gibbons at oracle.com>, "compiler-dev" <compiler-dev at openjdk.java.net>
> Envoyé: Mercredi 11 Décembre 2019 22:18:21
> Objet: Re: RFR: JDK-8235621: Replace use of Function<?,Integer> with ToIntFunction
> Looks good - while I understand the general point around contravariance,
> I think it's always a trade off between simplicity and completeness. In
> this case, using ? super buys you nothing (since there's no interesting
> Symbol supertype). Yes Remi, we can write examples where that bites, but
> I don't think that's important - and we can change if (and when) that
> becomes an issue.
>
> Maurizio
>
> On 10/12/2019 00:38, Jonathan Gibbons wrote:
>> Following Rémi's suggestion[1], please review a trivial change to
>> javac to have it use ToIntFunction
>> instead of Function<..., Integer> in the recent patch for JDK-8234689
>> [2].
>>
>> Patch inline below. No change to the original test is necessary.
>>
>> JBS: https://bugs.openjdk.java.net/browse/JDK-8235621
>> [1]
>> https://mail.openjdk.java.net/pipermail/compiler-dev/2019-December/014043.html
>> [2] https://hg.openjdk.java.net/jdk/jdk/rev/17b0f051280f
>>
>> -- Jon
>>
>> diff -r 2aaa8bcb90a9 -r 1e000f399f3a
>> src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
>>
>> ---
>> a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
>> Mon Dec 09 15:28:46 2019 +0100
>> +++
>> b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
>> Mon Dec 09 16:12:42 2019 -0800
>> @@ -30,7 +30,7 @@
>> import java.util.Map;
>> import java.util.Set;
>> import java.util.LinkedHashSet;
>> -import java.util.function.Function;
>> +import java.util.function.ToIntFunction;
>>
>> import javax.tools.JavaFileManager;
>> import javax.tools.FileObject;
>> @@ -114,7 +114,7 @@
>> */
>> public boolean multiModuleMode;
>>
>> - private List<Function<Symbol, Integer>> extraAttributeHooks =
>> List.nil();
>> + private List<ToIntFunction<Symbol>> extraAttributeHooks =
>> List.nil();
>>
>> /** The initial sizes of the data and constant pool buffers.
>> * Sizes are increased when buffers get full.
>> @@ -191,7 +191,7 @@
>> }
>> }
>>
>> - public void addExtraAttributes(Function<Symbol, Integer>
>> addExtraAttributes) {
>> + public void addExtraAttributes(ToIntFunction<Symbol>
>> addExtraAttributes) {
>> extraAttributeHooks =
>> extraAttributeHooks.prepend(addExtraAttributes);
>> }
>>
>> @@ -1669,7 +1669,7 @@
>> */
>> protected int writeExtraAttributes(Symbol sym) {
>> int i = 0;
>> - for (Function<Symbol, Integer> hook : extraAttributeHooks) {
>> + for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
>> i += hook.apply(sym);
>> }
>> return i;
More information about the compiler-dev
mailing list