There are no implicit conversions and implicit parameters in java
If you want to use dynamic proxy/AOP and other technologies in Java
Overview
The implicit conversion and implicit parameter functions provided by Scala are very distinctive, and they are also functions that are not available in programming languages such as Java.
You can convert a certain type of object into a specified type or add a new method to a type (you can add new functions to the code without modifying the original code)
Later in learning Akka, Spark, Flink, etc., you will see implicit conversions and implicit parameters, which can achieve very powerful and special functions.
Implicit conversion
The so-called implicit conversion refers to a method with a single parameter declared with the implicit keyword.
The defined implicit conversion method will be automatically invoked by Scala as long as it is imported in the written program.
According to the signature of the implicit conversion method, Scala will automatically pass it to the implicit conversion method when the object defined by the parameter type received by the implicit conversion method is used in the program, convert it to another type of object, and return it.
Implicit parameter
The so-called implicit parameter refers to the definition of an implicitly modified parameter in a function or method. At this time, the compiler will try to find a specified type of implicit value modified with implicit and set it to the parameter.
Scala will search in two scopes: the implicit variable defined by val or var visible in the current scope, and the implicit value in the companion object of the implicit parameter type.
Timing of implicit conversion
When the type of the parameter in the method is inconsistent with the target type
When an object calls a method or member that does not exist in the class, the compiler will automatically convert the object implicitly
Implicit conversion lookup and import
(1) Scala will use two implicit conversions by default,
One is the implicit conversion function or implicit value in the source type or the companion object of the target type;
One is the implicit conversion method or implicit value that can be represented by a unique identifier within the scope of the current program.
(2) If it is not in the above two cases,
Then you must manually use the import syntax to introduce implicit conversions under a certain package, such as import test._
It is generally recommended: Only use import to import implicit conversions where implicit conversions are required, so that the scope of implicit conversions can be narrowed, and unnecessary implicit conversions can be avoided
note
1. All the implicit values and implicit methods must be placed in the object.
2. The implicit keyword can only be used to modify methods, variables (parameters) and accompanying objects.
3. The Scala program will automatically import the following packages:
import java.lang._
import scala._
import Predef._
Implicit conversion
The implicit conversion in Scala can convert an object into another type according to the implicit conversion, which is convenient to use
In future development, we only need to import implicit conversions written by others.