RFR: JDK-8235621: Replace use of Function<?,Integer> with ToIntFunction
Jonathan Gibbons
jonathan.gibbons at oracle.com
Wed Dec 11 00:26:31 UTC 2019
Rémi,
I don't believe there are any valid use cases for the scenario you
describe, particularly with a lambda as simple as "(Object o) -> 12".
Not only are there no existing clients of the proposed API, since it is
new API, any realistic clients will want to access the symbol in order
to write the contents of the attribute. While there may be reasons in
more general contexts to use a super wildcard, I don't believe that
applies here, and I think it is better to support the simple
non-wildcard case instead of forcing wildcards on clients.
This proposed change is *just* about responding to your earlier
suggestion to replace Function<Symbol, Integer> with ToIntFunction<Symbol>.
-- Jon
On 12/09/2019 11:39 PM, forax at univ-mlv.fr wrote:
> ----- Mail original -----
>> De: "jonathan gibbons" <jonathan.gibbons at oracle.com>
>> À: "Remi Forax" <forax at univ-mlv.fr>
>> Cc: "compiler-dev" <compiler-dev at openjdk.java.net>
>> Envoyé: Mardi 10 Décembre 2019 02:28:23
>> Objet: Re: RFR: JDK-8235621: Replace use of Function<?,Integer> with ToIntFunction
>> Rémi,
>>
>> While I understand the use of wildcards for parameters and return types, I don't
>> think it makes sense to allow/use "? super Symbol" in this situation. I'd
>> prefer to keep it simple and just use ToIntFunction<Symbol>.
> but you are forcing people that that already have a ToIntFunction<super type of Symbol> to do a lambda to lambda conversion which is weird
>
> ToIntFunction<Object> extraAttributes = (Object o) -> 12;
> addExtraAttributes(extraAttributes::applyAsInt); // WTF ?
>
> instead of
>
> ToIntFunction<Object> extraAttributes = (Object o) -> 12;
> addExtraAttributes(extraAttributes);
>
>> -- Jon
> Rémi
>
>>
>> On 12/09/2019 05:20 PM, Remi Forax wrote:
>>> Hi Jonathan,
>>> for addExtraAttributes, the method should be
>>>
>>> public void addExtraAttributes(ToIntFunction<? super Symbol> addExtraAttributes)
>>> {
>>>
>>> for the same reason a public method should take a List and not an ArrayList, so
>>> you can call it with
>>> addExtraAttributes((Object o) -> 12);
>>> if the attribute is constant.
>>>
>>> If addExtraAttributes takes a ToIntFunction<? super Symbol>, you will have to
>>> use ToIntFunction<? super Symbol> everywhere.
>>>
>>> I should propose a new JEP to lock Dan Smith into a prison until JEP 300 [1] is
>>> delivered, because we will be able to get ride of most of those pesky wildcards
>>> (at least all the ones on functional interfaces) if that JEP is delivered.
>>>
>>> regards,
>>> Rémi
>>>
>>> [1] https://openjdk.java.net/jeps/300
>>>
>>> ----- Mail original -----
>>>> De: "jonathan gibbons" <jonathan.gibbons at oracle.com>
>>>> À: "compiler-dev" <compiler-dev at openjdk.java.net>
>>>> Envoyé: Mardi 10 Décembre 2019 01:38:44
>>>> Objet: RFR: JDK-8235621: Replace use of Function<?,Integer> with ToIntFunction
>>>> 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