How scala HashMap inherit the “++” method while both superclasses TraversableLike and MapLike has same...












1














When I was learning Scala source code of version 2.11.8, I found aspect of HashMap which confused me:



I noticed that there are 2 traits of HashMap for mixin directly or indirectly, which both defined ++ method:




  • TraversableLike

  • MapLike


It seems that these 2 traits both take GenTraversableOnce type as parameter, but with difference that TraversableLike trait defined one more parameter list for implicit CanBuildFrom, however in HashMap.scala, there is also defined an implicit CanBuildFrom in scope, but I have NO idea which ++ method HashMap will inherit from between these 2 traits, i.e. whether to use implicit CanBuildFrom or not. Please, see bellow my test example:



  //the paramter list for implicit ev is just evidence to limit the (A,B) as subtypes of (Int,String), to confirm with the expected return type.
def addElements1[A,B](t: (A,B)*)(implicit ev: <:<[(A,B),(Int,String)]): HashMap[Int,String] = {
val h = new HashMap[Int,String]
h ++ t
h
}

def addElements2[A,B](t: (A,B)*): HashMap[A,B] = {
val h = new HashMap[A,B]
h ++ t
h
}

def main(args: Array[String]): Unit = {
addElements1((1,"a"),(2,"b"),(3,"c"))
addElements2((4,"d"),(5,"e"),(6,"f"))
}


During debug process, I got different results of there 2 method, that is, the method addElements1 inherits the method ++ from trait TraversableLike. Method addElements2 inherits the method ++ from trait MapLike.



Is there obvious rule or condition for which method should me used for HashMap?



FYI: The inheritance chain of HashMap from these 2 traits are:



HashMap <- MapLike
HashMap <- Map <- Iterable <- Traversable <- TraversableLike


Thanks a lot for all your effort on this question in advance.










share|improve this question





























    1














    When I was learning Scala source code of version 2.11.8, I found aspect of HashMap which confused me:



    I noticed that there are 2 traits of HashMap for mixin directly or indirectly, which both defined ++ method:




    • TraversableLike

    • MapLike


    It seems that these 2 traits both take GenTraversableOnce type as parameter, but with difference that TraversableLike trait defined one more parameter list for implicit CanBuildFrom, however in HashMap.scala, there is also defined an implicit CanBuildFrom in scope, but I have NO idea which ++ method HashMap will inherit from between these 2 traits, i.e. whether to use implicit CanBuildFrom or not. Please, see bellow my test example:



      //the paramter list for implicit ev is just evidence to limit the (A,B) as subtypes of (Int,String), to confirm with the expected return type.
    def addElements1[A,B](t: (A,B)*)(implicit ev: <:<[(A,B),(Int,String)]): HashMap[Int,String] = {
    val h = new HashMap[Int,String]
    h ++ t
    h
    }

    def addElements2[A,B](t: (A,B)*): HashMap[A,B] = {
    val h = new HashMap[A,B]
    h ++ t
    h
    }

    def main(args: Array[String]): Unit = {
    addElements1((1,"a"),(2,"b"),(3,"c"))
    addElements2((4,"d"),(5,"e"),(6,"f"))
    }


    During debug process, I got different results of there 2 method, that is, the method addElements1 inherits the method ++ from trait TraversableLike. Method addElements2 inherits the method ++ from trait MapLike.



    Is there obvious rule or condition for which method should me used for HashMap?



    FYI: The inheritance chain of HashMap from these 2 traits are:



    HashMap <- MapLike
    HashMap <- Map <- Iterable <- Traversable <- TraversableLike


    Thanks a lot for all your effort on this question in advance.










    share|improve this question



























      1












      1








      1







      When I was learning Scala source code of version 2.11.8, I found aspect of HashMap which confused me:



      I noticed that there are 2 traits of HashMap for mixin directly or indirectly, which both defined ++ method:




      • TraversableLike

      • MapLike


      It seems that these 2 traits both take GenTraversableOnce type as parameter, but with difference that TraversableLike trait defined one more parameter list for implicit CanBuildFrom, however in HashMap.scala, there is also defined an implicit CanBuildFrom in scope, but I have NO idea which ++ method HashMap will inherit from between these 2 traits, i.e. whether to use implicit CanBuildFrom or not. Please, see bellow my test example:



        //the paramter list for implicit ev is just evidence to limit the (A,B) as subtypes of (Int,String), to confirm with the expected return type.
      def addElements1[A,B](t: (A,B)*)(implicit ev: <:<[(A,B),(Int,String)]): HashMap[Int,String] = {
      val h = new HashMap[Int,String]
      h ++ t
      h
      }

      def addElements2[A,B](t: (A,B)*): HashMap[A,B] = {
      val h = new HashMap[A,B]
      h ++ t
      h
      }

      def main(args: Array[String]): Unit = {
      addElements1((1,"a"),(2,"b"),(3,"c"))
      addElements2((4,"d"),(5,"e"),(6,"f"))
      }


      During debug process, I got different results of there 2 method, that is, the method addElements1 inherits the method ++ from trait TraversableLike. Method addElements2 inherits the method ++ from trait MapLike.



      Is there obvious rule or condition for which method should me used for HashMap?



      FYI: The inheritance chain of HashMap from these 2 traits are:



      HashMap <- MapLike
      HashMap <- Map <- Iterable <- Traversable <- TraversableLike


      Thanks a lot for all your effort on this question in advance.










      share|improve this question















      When I was learning Scala source code of version 2.11.8, I found aspect of HashMap which confused me:



      I noticed that there are 2 traits of HashMap for mixin directly or indirectly, which both defined ++ method:




      • TraversableLike

      • MapLike


      It seems that these 2 traits both take GenTraversableOnce type as parameter, but with difference that TraversableLike trait defined one more parameter list for implicit CanBuildFrom, however in HashMap.scala, there is also defined an implicit CanBuildFrom in scope, but I have NO idea which ++ method HashMap will inherit from between these 2 traits, i.e. whether to use implicit CanBuildFrom or not. Please, see bellow my test example:



        //the paramter list for implicit ev is just evidence to limit the (A,B) as subtypes of (Int,String), to confirm with the expected return type.
      def addElements1[A,B](t: (A,B)*)(implicit ev: <:<[(A,B),(Int,String)]): HashMap[Int,String] = {
      val h = new HashMap[Int,String]
      h ++ t
      h
      }

      def addElements2[A,B](t: (A,B)*): HashMap[A,B] = {
      val h = new HashMap[A,B]
      h ++ t
      h
      }

      def main(args: Array[String]): Unit = {
      addElements1((1,"a"),(2,"b"),(3,"c"))
      addElements2((4,"d"),(5,"e"),(6,"f"))
      }


      During debug process, I got different results of there 2 method, that is, the method addElements1 inherits the method ++ from trait TraversableLike. Method addElements2 inherits the method ++ from trait MapLike.



      Is there obvious rule or condition for which method should me used for HashMap?



      FYI: The inheritance chain of HashMap from these 2 traits are:



      HashMap <- MapLike
      HashMap <- Map <- Iterable <- Traversable <- TraversableLike


      Thanks a lot for all your effort on this question in advance.







      scala hashmap scala-collections






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 11:07









      ygor

      911514




      911514










      asked Nov 23 at 10:06









      Jason L

      111




      111





























          active

          oldest

          votes











          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%2f53444567%2fhow-scala-hashmap-inherit-the-method-while-both-superclasses-traversablelik%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53444567%2fhow-scala-hashmap-inherit-the-method-while-both-superclasses-traversablelik%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)