An in-depth understanding of the implicit conversion system in Scala

Source: Internet
Author: User

Blog Core content:
1. Two implicit conversion mechanisms in Scala and how implicit views are defined
2. Where the implicit binding in Scala might be located and how to use implicit conversions better
3. Implicit conversion related operation rules in Scala
4. Implicit parameters in Scala
5. Implicit classes in Scala
6. An implicit object in Scala
7. Two implicit type constraints in Scala (combined with the type system in Scala)
8. Introduction to the usage of the implicitly method in the Predef class and the way ordering type is converted to oredered type

1. Two implicit conversion mechanisms in Scala and how implicit views are defined

⑴scala supports two forms of implicit conversion mechanisms-implicit values and implicit views:
① implicit values can be used to provide parameters to methods
② implicit view refers to the automatic conversion of one type to another, and the use of properties and methods in another type to satisfy the requirements of an expression.
The definition of an implicit view is generally defined in the following form:

Effect: If this definition exists in an implicit scope, it implicitly converts the value of the original type to the value of the enhanced type (when needed).
⑵ implicit and implicit views all use the same implicit parsing mechanism, so-called implicit parsing refers to the process of finding missing information if the compiler discovers that some information is missing from the code. In Scala, implicit parsing is generally divided into two stages: in the scope lookup of an implicit conversion, if the current scope does not have an implicit conversion, the compiler automatically finds an implicit conversion to the associated object of the corresponding source or target type.
⑶ in Scala, the implicit view, the name of an implicit function, is generally written like this: the previous type before the conversion, followed by the enhanced type, such as Int2integer. and the parameter of the function is generally an instance of the class before the conversion, and the execution body of the function is to create an enhanced class object.


⑷ in implicit view, the parameter type of the main constructor of the enhanced class is generally the type of the class before the conversion, and the enhanced class contains the method we want.

Example Program 1:

2. Where the implicit binding in Scala might be located and how to use implicit conversions better

⑴ the most important thing to do when applying an implicit system is to make sure that the programmer understands what's going on in a piece of code. To do this, you must narrow down the range of code that the programmer needs to see when it finds the available implicit values. Implicit binding may be in place:
① associated objects for all associated types, including package objects: That is, Scala looks for implicit values in the associated object of the association type, which is the core of the Scala language.
②scala.predef header file: Each compiled Scala file begins with a sentence:

The Predef header file contains a number of useful conversion methods, such as having a Java.lang.Integer =>scala in the scala.predef header file. An implicit conversion of int.
③ all import statements in the scope: the last implicit conversion might exist in the source code with the explicit import imports statement.
How to use implicit conversions better in ⑵scala: with the associated class and the associated object mechanism, the compiler will automatically import implicit conversions without the user having to manually import the implicit conversions.

Instance Program 1:

Instance Program 2:

3. Implicit conversion related operation rules in Scala

① in Scala, to use implicit conversions, you must mark the implicit keyword, which can be used to decorate parameters (implicit and implicit), functions (implicit views), classes (implicit classes), objects (implicit objects).
② implicit conversions must be a single identifier throughout the scope to avoid implicit collisions.
③ in the scope lookup of an implicit conversion, if the current scope does not have an implicit conversion, the compiler automatically looks for an implicit conversion in the associated object of the corresponding source or target type.
Implicit values, implicit classes, implicit objects in ④scala are often placed in a singleton object.

Instance Program 1:

4. Implicit parameters in Scala

① in Scala, the so-called implicit argument is to precede the arguments in a function or method with the implicit modifier, which is called an implicit parameter.
The ② implicit parameter provides a great mechanism for the user to specify no redundant parameters.
③ in Scala, if the function has a implicit modifier in front of its arguments, the compiler will automatically look for the corresponding implicit value to pass as the default value (the default).

5. Implicit classes in Scala

The implicit class in ①scala is to precede the ordinary class with a implicit keyword, and modify the corresponding main constructor, the parameter type of the main constructor is generally the type of the class before the conversion, and finally import within the implicit scope. The implicit classes in Scala are a form of enhanced class functionality.
The implicit view in ②scala is a fuss in the companion object of the class before the conversion, and the implicit class in Scala is a fuss in the enhanced class, because the former does not have to be imported manually, so the implicit classes in Scala are less used than implicit views.
③ can build static properties and methods in singleton objects, and can also build static classes, and if you want to use static classes in Singleton objects, import imports.

Instance Program 1:

6. An implicit object in Scala

An implicit object in ①scala is the addition of the implicit modifier in front of an object object, which, to some extent, can be used as an implicit value.
② in Scala, when labeling types, you can refer to their type directly with the name of the class and trait, and to reference the type of the object, you need to use the type member of the object to refer to it, instead of referencing its type directly with the name of object, mainly to distinguish the associated object from the associated class.

Instance Program 1:

7. Two implicit type constraints in Scala (combined with the type system in Scala)

⑴scala supports two special types of constraint operators, which are not real type constraints but implicit lookups, both contextual and view-defined. These operators allow you to define an implicit parameter list as a type constraint for a generic type. In cases where the implicit conversion definition must be found but does not require direct access, this syntax allows you to do less typing. The two types of implicit type constraints are view definitions (which involve implicit views) and context definitions (implicit values).
The ① view definition is used to "require" an available implicit view (internal mechanism) to automatically convert one type to another type. An implicit view is defined as follows:

Meaning: The Foo method defines a constraint a <% B, which means that the type of parameter x is a, and that there must be an implicit conversion a=>b in place of the call, which can be expressed in the following notation:

This Foo method can also be defined with a type parameter without a type constraint. There are two parameters: one accepts a parameter of type A, and the second accepts an implicit conversion function. Although the second form would be more words, he gave the implicit conversion function a user-defined label.
The ② context definition is used to "require" an available implicit value to exist and must exist. The context is defined as follows:

The Foo method defines the constraint a:b, which means that the type of parameter x is a, and that the Foo method must have an available implicit value b[a] present, and the context definition can be re-written:

This Foo method defines two parameters whose implicit arguments accept a value of type b[a]. Although the second form would be more words, he gave the implicit value a user-defined label.

Instance Program 1:

Instance Program 2:

8. Introduction to the usage of the implicitly method in the Predef class and the way ordering type is converted to oredered type

The implicitly method in the ①predef class can get the actual value of the parameter when it is run:

The associated object of ②ordered contains a lot of content: one is to convert the ordering data type implicitly to the oredered type:

Instance Program 1:

Instance Program 2:

If you have any questions, please feel free to comment!!

An in-depth understanding of the implicit conversion system in Scala

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.