Is List f() a useful signature? Is there any problem with it / using it?












9














Is <T> List<? extends T> f() a useful signature? Is there any problem with it / using it?



This was an interview question. I know this:




  1. It compiles fine

  2. Use it like List<? extends Number> lst = obj.<Number>f(), and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)


Does that fully answer the question?










share|improve this question






















  • Returning a wildcard is not really useful
    – NEGR KITAEC
    56 mins ago










  • You can still call add(null) etc.
    – Andy Turner
    51 mins ago












  • Related stackoverflow.com/questions/918026/list-extends-mytype
    – user7294900
    51 mins ago












  • Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
    – DodgyCodeException
    49 mins ago
















9














Is <T> List<? extends T> f() a useful signature? Is there any problem with it / using it?



This was an interview question. I know this:




  1. It compiles fine

  2. Use it like List<? extends Number> lst = obj.<Number>f(), and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)


Does that fully answer the question?










share|improve this question






















  • Returning a wildcard is not really useful
    – NEGR KITAEC
    56 mins ago










  • You can still call add(null) etc.
    – Andy Turner
    51 mins ago












  • Related stackoverflow.com/questions/918026/list-extends-mytype
    – user7294900
    51 mins ago












  • Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
    – DodgyCodeException
    49 mins ago














9












9








9


1





Is <T> List<? extends T> f() a useful signature? Is there any problem with it / using it?



This was an interview question. I know this:




  1. It compiles fine

  2. Use it like List<? extends Number> lst = obj.<Number>f(), and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)


Does that fully answer the question?










share|improve this question













Is <T> List<? extends T> f() a useful signature? Is there any problem with it / using it?



This was an interview question. I know this:




  1. It compiles fine

  2. Use it like List<? extends Number> lst = obj.<Number>f(), and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)


Does that fully answer the question?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 59 mins ago









user10777718

564




564












  • Returning a wildcard is not really useful
    – NEGR KITAEC
    56 mins ago










  • You can still call add(null) etc.
    – Andy Turner
    51 mins ago












  • Related stackoverflow.com/questions/918026/list-extends-mytype
    – user7294900
    51 mins ago












  • Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
    – DodgyCodeException
    49 mins ago


















  • Returning a wildcard is not really useful
    – NEGR KITAEC
    56 mins ago










  • You can still call add(null) etc.
    – Andy Turner
    51 mins ago












  • Related stackoverflow.com/questions/918026/list-extends-mytype
    – user7294900
    51 mins ago












  • Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
    – DodgyCodeException
    49 mins ago
















Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago




Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago












You can still call add(null) etc.
– Andy Turner
51 mins ago






You can still call add(null) etc.
– Andy Turner
51 mins ago














Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago






Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago














Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
– DodgyCodeException
49 mins ago




Using type inference you can just do List<? extends Number> lst = s.f();. Also, you can do Number number = lst.get(0); System.out.println(number);.
– DodgyCodeException
49 mins ago












3 Answers
3






active

oldest

votes


















3














I interpret "is it a useful signature" to mean "can you think of a use-case for it".



T is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.



Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.





Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.






share|improve this answer























  • Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
    – DodgyCodeException
    44 mins ago



















0














I'd consider it not useful because List<? extends T> gives you the same amount of information List<T> does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T> you wont be able to use your List<? extends T> even so what ever the function does would work just fine as there are Ts in your list.



Just using List<T> is better.



EDIT



This answer is more general about using List and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.






share|improve this answer










New contributor




Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    0














    The official Generics tutorial suggests not to use wildcard return types.



    "These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."



    The reasoning given isn't exactly convincing, though.






    share|improve this answer





















      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',
      autoActivateHeartbeat: false,
      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%2f54004821%2fis-t-list-extends-t-f-a-useful-signature-is-there-any-problem-with-it%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      I interpret "is it a useful signature" to mean "can you think of a use-case for it".



      T is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.



      Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.





      Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.






      share|improve this answer























      • Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
        – DodgyCodeException
        44 mins ago
















      3














      I interpret "is it a useful signature" to mean "can you think of a use-case for it".



      T is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.



      Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.





      Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.






      share|improve this answer























      • Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
        – DodgyCodeException
        44 mins ago














      3












      3








      3






      I interpret "is it a useful signature" to mean "can you think of a use-case for it".



      T is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.



      Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.





      Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.






      share|improve this answer














      I interpret "is it a useful signature" to mean "can you think of a use-case for it".



      T is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.



      Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.





      Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 43 mins ago

























      answered 47 mins ago









      Andy Turner

      80.3k879133




      80.3k879133












      • Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
        – DodgyCodeException
        44 mins ago


















      • Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
        – DodgyCodeException
        44 mins ago
















      Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
      – DodgyCodeException
      44 mins ago




      Well, looking very desperately for a contrived way of making it useful, you could have called a setReturnType(Class<?> cls) method on the same object beforehand, and then f() would use that information to return a list of the required type.
      – DodgyCodeException
      44 mins ago













      0














      I'd consider it not useful because List<? extends T> gives you the same amount of information List<T> does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T> you wont be able to use your List<? extends T> even so what ever the function does would work just fine as there are Ts in your list.



      Just using List<T> is better.



      EDIT



      This answer is more general about using List and wildcards.
      @Andy Turner gave a more specific (and probably better) answer to the question.






      share|improve this answer










      New contributor




      Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0














        I'd consider it not useful because List<? extends T> gives you the same amount of information List<T> does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T> you wont be able to use your List<? extends T> even so what ever the function does would work just fine as there are Ts in your list.



        Just using List<T> is better.



        EDIT



        This answer is more general about using List and wildcards.
        @Andy Turner gave a more specific (and probably better) answer to the question.






        share|improve this answer










        New contributor




        Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





















          0












          0








          0






          I'd consider it not useful because List<? extends T> gives you the same amount of information List<T> does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T> you wont be able to use your List<? extends T> even so what ever the function does would work just fine as there are Ts in your list.



          Just using List<T> is better.



          EDIT



          This answer is more general about using List and wildcards.
          @Andy Turner gave a more specific (and probably better) answer to the question.






          share|improve this answer










          New contributor




          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          I'd consider it not useful because List<? extends T> gives you the same amount of information List<T> does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T> you wont be able to use your List<? extends T> even so what ever the function does would work just fine as there are Ts in your list.



          Just using List<T> is better.



          EDIT



          This answer is more general about using List and wildcards.
          @Andy Turner gave a more specific (and probably better) answer to the question.







          share|improve this answer










          New contributor




          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 44 mins ago





















          New contributor




          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 50 mins ago









          Kanjiu

          2369




          2369




          New contributor




          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Kanjiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.























              0














              The official Generics tutorial suggests not to use wildcard return types.



              "These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."



              The reasoning given isn't exactly convincing, though.






              share|improve this answer


























                0














                The official Generics tutorial suggests not to use wildcard return types.



                "These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."



                The reasoning given isn't exactly convincing, though.






                share|improve this answer
























                  0












                  0








                  0






                  The official Generics tutorial suggests not to use wildcard return types.



                  "These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."



                  The reasoning given isn't exactly convincing, though.






                  share|improve this answer












                  The official Generics tutorial suggests not to use wildcard return types.



                  "These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."



                  The reasoning given isn't exactly convincing, though.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 15 mins ago









                  togorks

                  285




                  285






























                      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%2f54004821%2fis-t-list-extends-t-f-a-useful-signature-is-there-any-problem-with-it%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

                      Understanding the information contained in the Deep Space Network XML data?

                      Ross-on-Wye

                      Eastern Orthodox Church