Why Optional's or and flatMap method's supplier type parameter is a wildcard











up vote
6
down vote

favorite
1












The Optional.or method was added in Java 9. This is the method signature



public Optional<T> or​(Supplier<? extends Optional<? extends T>> supplier)


Why is the type parameter of the Supplier taking ? extends Optional rather than just Optional since Optional is a final class.



The same is true for Optional.flatMap method. This is a change from Java 8.



In Java 8, it was Function<? super T, Optional<U>> mapper wherease it was changed to Function<? super T,​? extends Optional<? extends U>> in Java 9.










share|improve this question
























  • Kinda seems like they're positioning it to not be a final class in future version?
    – nbrooks
    5 hours ago










  • @nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
    – user7
    5 hours ago















up vote
6
down vote

favorite
1












The Optional.or method was added in Java 9. This is the method signature



public Optional<T> or​(Supplier<? extends Optional<? extends T>> supplier)


Why is the type parameter of the Supplier taking ? extends Optional rather than just Optional since Optional is a final class.



The same is true for Optional.flatMap method. This is a change from Java 8.



In Java 8, it was Function<? super T, Optional<U>> mapper wherease it was changed to Function<? super T,​? extends Optional<? extends U>> in Java 9.










share|improve this question
























  • Kinda seems like they're positioning it to not be a final class in future version?
    – nbrooks
    5 hours ago










  • @nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
    – user7
    5 hours ago













up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





The Optional.or method was added in Java 9. This is the method signature



public Optional<T> or​(Supplier<? extends Optional<? extends T>> supplier)


Why is the type parameter of the Supplier taking ? extends Optional rather than just Optional since Optional is a final class.



The same is true for Optional.flatMap method. This is a change from Java 8.



In Java 8, it was Function<? super T, Optional<U>> mapper wherease it was changed to Function<? super T,​? extends Optional<? extends U>> in Java 9.










share|improve this question















The Optional.or method was added in Java 9. This is the method signature



public Optional<T> or​(Supplier<? extends Optional<? extends T>> supplier)


Why is the type parameter of the Supplier taking ? extends Optional rather than just Optional since Optional is a final class.



The same is true for Optional.flatMap method. This is a change from Java 8.



In Java 8, it was Function<? super T, Optional<U>> mapper wherease it was changed to Function<? super T,​? extends Optional<? extends U>> in Java 9.







java optional java-9 supplier






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









nullpointer

38k1073145




38k1073145










asked 6 hours ago









user7

8,89132040




8,89132040












  • Kinda seems like they're positioning it to not be a final class in future version?
    – nbrooks
    5 hours ago










  • @nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
    – user7
    5 hours ago


















  • Kinda seems like they're positioning it to not be a final class in future version?
    – nbrooks
    5 hours ago










  • @nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
    – user7
    5 hours ago
















Kinda seems like they're positioning it to not be a final class in future version?
– nbrooks
5 hours ago




Kinda seems like they're positioning it to not be a final class in future version?
– nbrooks
5 hours ago












@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
5 hours ago




@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
5 hours ago












1 Answer
1






active

oldest

votes

















up vote
9
down vote













I found the reasoning behind this from Stuart Marks himself



http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html



This has to do with nested generics (Optional is nested within Function).
From the mail thread



  Function<..., Optional<StringBuilder>>

is not a subtype of

Function<..., Optional<? extends CharSequence>>

To get around this, we have to add the outer wildcard as well, so that

Function<..., Optional<StringBuilder>>

is a subtype of

Function<..., ? extends Optional<? extends CharSequence>>




share





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53698401%2fwhy-optionals-or-and-flatmap-methods-supplier-type-parameter-is-a-wildcard%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    9
    down vote













    I found the reasoning behind this from Stuart Marks himself



    http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html



    This has to do with nested generics (Optional is nested within Function).
    From the mail thread



      Function<..., Optional<StringBuilder>>

    is not a subtype of

    Function<..., Optional<? extends CharSequence>>

    To get around this, we have to add the outer wildcard as well, so that

    Function<..., Optional<StringBuilder>>

    is a subtype of

    Function<..., ? extends Optional<? extends CharSequence>>




    share

























      up vote
      9
      down vote













      I found the reasoning behind this from Stuart Marks himself



      http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html



      This has to do with nested generics (Optional is nested within Function).
      From the mail thread



        Function<..., Optional<StringBuilder>>

      is not a subtype of

      Function<..., Optional<? extends CharSequence>>

      To get around this, we have to add the outer wildcard as well, so that

      Function<..., Optional<StringBuilder>>

      is a subtype of

      Function<..., ? extends Optional<? extends CharSequence>>




      share























        up vote
        9
        down vote










        up vote
        9
        down vote









        I found the reasoning behind this from Stuart Marks himself



        http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html



        This has to do with nested generics (Optional is nested within Function).
        From the mail thread



          Function<..., Optional<StringBuilder>>

        is not a subtype of

        Function<..., Optional<? extends CharSequence>>

        To get around this, we have to add the outer wildcard as well, so that

        Function<..., Optional<StringBuilder>>

        is a subtype of

        Function<..., ? extends Optional<? extends CharSequence>>




        share












        I found the reasoning behind this from Stuart Marks himself



        http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html



        This has to do with nested generics (Optional is nested within Function).
        From the mail thread



          Function<..., Optional<StringBuilder>>

        is not a subtype of

        Function<..., Optional<? extends CharSequence>>

        To get around this, we have to add the outer wildcard as well, so that

        Function<..., Optional<StringBuilder>>

        is a subtype of

        Function<..., ? extends Optional<? extends CharSequence>>





        share











        share


        share










        answered 5 hours ago









        user7

        8,89132040




        8,89132040






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53698401%2fwhy-optionals-or-and-flatmap-methods-supplier-type-parameter-is-a-wildcard%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt