A detailed explanation of map initialization process in Scala and implicit type conversion __spark

Source: Internet
Author: User
Tags instance method

In Scala, you can initialize a map object in this way:

[Java] view plain copy var capital = Map ("US"-> "Washington", "France"-> "Paris")

This way of creating a Map object gives an elegant feeling and has to admire the imagination of the Scala language author. But how does this initialization approach be achieved? -> is an operator. is still a method. If it is a method, and there is no such method on the string object, and there is no such method on the object, then how does the string "US" Invoke the-> method?


With these questions, let's write an example to verify how this initialization is implemented. The sample code is as follows:

[Java] view Plain Copy object main {def Main (args:array[string]) {var capital = Map ("US"-&G T "Washington", "France"-> "Paris"}}

There is only one line of code in the entry function that creates a map object in the way described above. In our previous blog, we talked about a single instance object decorated with the object keyword, and a fictional class after the single object was compiled into a class file. The name of the fictitious class is Main$.class. A fictional class has a member method main with the same name. The main logic of the Scala entry function is in this main method. As for the implementation of the single example object, there are several articles that have been introduced before, and no longer repeat here. Unclear readers can refer to a few previous blogs:

Learn Scala: Starting from HelloWorld

Learning Scala: How orphaned objects are implemented

Learning Scala: How the companion object works


We know that the logic to create a map object is compiled into the Main$.class main instance method.  Here we decompile the Main$.class to see how it is implemented. The following is the bytecode of the main method in Main$.class after the decompile:

[Java]  View Plain  copy   Public void main (java.lang.string[]);       flags: acc_public      Code:        stack=8,  locals=3, args_size=2           0: getstatic       #19                   // field scala/predef$. module$:lscala/predef$;           3: invokevirtual  #23                  //  method scala/predef$. Map: () lscala/collection/immutable/map$;           6:  getstatic      #19                   // field scala/predef$. module$:lscala/predef$;           9: iconst_2           10: anewarray      #25                   // class  scala/Tuple2          13: dup           14: iconst_0          15:  getstatic      #30                   // field scala/predef$arrowassoc$. module$:lscala/predef$arrowassoc$;          18: getstatic       #19                   // field scala/predef$. module$:lscala/predef$;          21: ldc             #32                   // String US           23: invokevirtual  #36                   // METHOD SCALA/PREDEF$.ANY2ARROWASSOC: (Ljava/lang/Object ;) ljava/lang/object;          26: ldc             #38                   // String Washington           28: invokevirtual  #42                   // method scala/predef$arrowassoc$. $minus $ Greater$extension: (Ljava/lang/object; Ljava/lang/object) lscala/tuple2;          31

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.