Suppose we have a class linenumber that represents the number of lines of text:
Class
We can use this class to represent the number of rows per page in a book:
New LineNumber (new linenumber (120)
The above code represents the number of rows for the first and second pages, respectively. Of course, we should also be able to add them together to get the total number of the two pages:
Val totallinenum = lineNumOfPage1 + lineNumOfPage2
In this case, there should be a "+" method in our LineNumber class to add two objects:
class linenumber (Val num:int) { newthis. num + that.num)}
Intuitively, we can even add to linenumber directly with integers:
- // or lineNumOfPage2 +
As we have written here, we find that the LineNumber object does not accept the "+" method of integral type as a parameter, and there is no "+" method that accepts linenumber as a parameter in an integral type.
To illustrate the problem, instead of adding an integer as an overloaded method in the LineNumber class, we define two implicit conversions, one converting int to linenumber, and the other converting linenumber to int:
Object linenumber{ implicitnew linenumber (i) implicit def Linenumbertoint (o:linenumber) = O.num}
Then let's try adding linenumber and integers:
+ lineNumOfPage2
Let's take a look at the results of the above two sentences after adding "-xprint:typer" at compile time:
Val totallinenumber1:linenumber = linenumofpage1.+ (linenumber.inttolinenumber. + (Linenumber.linenumbertoint (LineNumOfPage2));
It's clear to be here. The Scala compiler takes precedence over the parameters of the method as the transformation object, without selecting the object that invokes the method. What is this for? In fact, when the program type checks for problems, the Scala compiler tries to use implicit conversions in two locations for the statement that is having the problem:
- The first position is where you can convert a type directly to the desired type
- The second position is where you can convert the object of the calling method to the appropriate type.
The first position is the one where we need a but the incoming is B. The above example is precisely the case, for TotalLineNumber1, the LineNumOfPage1 "+" method requires the parameter type is linenumber type, but we passed the int type, the Scala compiler here to detect the type error, Will look for an implicit conversion to convert int to linenumber. Because at the beginning of the code, we imported the corresponding implicit conversions using "Import Linenumber._", so the Scala compiler uses the imported implicit conversion to convert the integer 120 to the linenumber type. TotalLineNumber2 occurs in the same way, except that the linenumber type is converted to the int type.
That is, when the Scala compiler uses implicit conversions, it first goes back to finding a place where the type can be converted directly, followed by the object that invokes the method. If we remove the conversion of inttolinenumber in our implicit conversion, then the totalLineNumber1 we get will become an integral type. Transferred from: http://blog.csdn.net/nethibernate/article/details/5893184
Scala's implicit conversion implicit detailed