Sometimes when we look at Scala code, we see that there is no type or function of the class, or even without the class, not only does the compiler report no errors, but the program runs just fine. In fact, we use Scala’s implicit conversion.

Implicit function

When we call a method on an object, but the object does not have the method, an implicit conversion is triggered, which surreptitiously converts the object to the object with the method. For example, a chicken can walk or run, but it also wants to fly like a bird, but it does not have this function. ChickenImplicit allows the chick to call the Bird’s fly method implicitly, allowing it to fly like a Bird.

object ImplicitDemo1 { def main(args: Array[String]): Unit = {import chickenImplicit._ val chicken = new chicken () chicken.work() chicken.run() // Fly ()}} class chicken () {def work(): Unit = {println(" chicken ")} def run(): Class Bird {def fly(): {def fly(): Unit = {println(" Bird is flying ")}} object ChickenImplicit {implicit def chicken2Bird(c: Chicken): Bird = new Bird()}

This is an additional definition called chickenImplicit, but sometimes we can define it directly in the same class without having to import it, depending on the actual situation.

object ImplicitDemo2 { def main(args: Array[String]): Unit = {val chicken = new Chicken2() chicken.work() chicken.run() And call Bird's fly method chick.fly ()} implicit def chicken2Bird(C: Chicken2): Bird2 = new Bird2()} class Chicken2() {def work(): Unit = {println()} def run(): Class Bird2 {def fly(): Unit = {println()}} class Bird2 {def fly(): Unit = {println()}}

Implicit parameter

Let’s say we want to call a method and print the argument that we passed, and we write it like this:

def main(args: Array[String]): Unit = {
    printInfo1("aaa") // aaa
}

def printInfo1(str: String): Unit = {
    println(str)
}

If we call a method and don’t want to pass an argument, we can do this: specify the default value in the method

def main(args: Array[String]): Unit = {
    printInfo2("bbb") // bbb
    printInfo2() // default
}

def printInfo2(str: String = "default"): Unit = {
    println(str)
}

If we use implicit arguments, then we can do this: the method here can be done without parentheses. Because implicit values are matched by types, the same type can only be used once in a scope, and its precedence is higher than the default, so it prints implicit when no arguments are passed.

def main(args: Array[String]): Unit = {
    implicit val str: String = "implicit"
    printInfo3("ccc") // ccc
    printInfo3() // default
    printInfo3 // implicit
}

def printInfo3(implicit str: String = "default"): Unit = {
    println(str)
}

conclusion

When we need an implicit conversion, we need to use the keyword implicit. To do an implicit conversion, he first looks globally to see if there is a matching method or argument, and then considers the implicit conversion if it is not. Since implicit parameters are type-by-type, multiple parameters of the same type cannot be defined in the same scope, and the default values take precedence over the default values.