Scala offers an implicit conversion and implicit parameter functionality that is very feature-specific. is a feature that is not in programming languages such as Java. It allows you to manually specify that an object of some type be converted to another type of object. With these features, you can achieve very powerful, and special features.
In Scala's implicit conversion, the most important thing is to define the implicit conversion function, that is, implicit conversion functions. The implicit conversion function that is defined is automatically used by Scala as long as it is introduced within the programmed program. Scala will automatically pass in an implicit conversion function, converting it to an object of another type and returning it, based on the signature of the implicit conversion function, when using the object defined by the parameter type that the implicit conversion function receives in the program. This is an implicit conversion.
It does not matter what the implicit conversion function is called because it is not usually called manually by the user, but is called by Scala. However, if you want to use an implicit conversion, you need to import the implicit conversion function. It is therefore generally recommended that the name of the implicit conversion function be named "One2One".
Implicit conversions
Case: Special Ticketing window (only for special groups such as students, seniors, etc.)
classSpecialperson (Val name:string)classStudent (Val name:string)classolder (Val name:string) implicit def Object2specialperson (obj:object): Specialperson= { if(Obj.getclass = = Classof[student]) {val stu = obj.asinstanceof[student];NewSpecialperson (stu.name)}Else if(Obj.getclass = = Classof[older]) {val older = Obj.asinstanceof[older];NewSpecialperson (older.name)}ElseNil} var ticketnumber= 0def buyspecialticket (P:specialperson)={ticketnumber+ = 1 "T" +Ticketnumber}
Using implicit conversions to enforce existing types
One of the most powerful features of implicit conversions is the ability to enhance the functionality of existing types unconsciously. That is, you can define a reinforced class for a class and define implicit conversions to each other, so that the source class is automatically converted to the enhanced class by Scala when using the enhanced version of the method, and then the method is called.
Case: Superman Change body
class Man (Val name:string) class Superman (Val name:string) { = println ("Emit a laster!" newnew man ("Leo") Leo.emitlaser
Implicit conversion function scope and import
Scala uses two implicit conversions by default, a source type, or an implicit conversion function within a companion object of the target type, and an implicit conversion function that can be represented in the current program scope with a unique identifier.
If the implicit conversion function is not in either of these cases, you must manually use import syntax to introduce an implicit conversion function under a package, such as import test._. It is generally recommended to use Iimport to import implicit conversion functions only where implicit conversions are required, such as in a function or method, to reduce the scope of the implicit conversion function and avoid unwanted implicit conversions.
Timing of implicit conversions
1. Call a function, but the type of the parameter passed in to the function does not match the type of the receive parameter defined by the function (case: Special ticket window)
2. Use an object of a certain type to invoke a method that does not exist at that type (case: Superman change)
3. Use an object of a certain type to invoke a method, although the type has this method, but the type of the parameter passed to the method does not match the type of the receive parameter defined by the method (case: Special Ticket Window Enhanced Edition)
Case: Special Ticket Window Enhanced Edition
class Tickethouse { = 0 = { + = 1 "T" + Ticketnumber }}
Implicitly-parameter
The so-called implicit parameter, which refers to a function or method, defines a parameter modified with implicit, when Scala attempts to find a specified type of implicit-decorated object, that is, an implicit value, and injects parameters.
Scala looks for two scopes: one is the implicit variable defined by Val or var that is visible within the current scope, and an implicit value within the associated object of the implicit parameter type.
Case: Exam Registration
class Signpen { =new signpendef Signforexam (name:string) (implicit signpen:signpen) { c6/>+ "come to exam in time." )}
Implicit conversions and implicit arguments