Solved my confusion about Classtag in Scala.

Source: Internet
Author: User
1> mainfest Context Definition
1. In Scala, arrays have to be typed, and if they are directly generic, they will get an error, which introduces the manifest context definition, requires a mainfest[t] object, and Mainifest[t] has an implicit value,
2. If Makepair is called, the compiler navigates to an implicit manifst[int] and actually calls Makepair (2,3) (intmanifest), which is called by the new Array (2) (intmanifest), Returns the base type of the array int[2]
3. An implicit manifest type is created in the new Array t
4. Why do manifest? Because the array is created, the compiler simply creates it and does not specify a type.

2> Classtag
1. The interpretation in the source code, in generics, type T is erased. This is very useful when we build an array, but we don't know the type of the array, but we don't know it when we compile it, but when we run it, we know that Classtag only contains the type of the actual runtime class.

2. Classtag will help us to store the information of T, such as the following, pass-through to the type can be specified as T is the int type, this time Classtag can pass the int type this information to the compiler. The Classtag runtime specifies type information that cannot be specified at compile time.

3. No need to write this, implicit m:manifest[t]

As you can see in the code below, the compiler will translate it into implicit because there are implicit and implicit values in the middle of the classtag, because we don't have to write that way directly classtag.

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.