Scala programming implicit conversion and implicit parameters (9)

Scala programming implicit conversion and implicit parameter introduction

The implicit conversion and implicit parameter functions provided by Scala are very distinctive functions. It is a feature that programming languages ​​such as Java do not have. It allows you to manually specify to convert certain types of objects into other types of objects. Through these functions, very powerful and special functions can be realized.

Scala's implicit conversion, in fact, the core is to define an implicit conversion function, that is, implicit conversion function. The defined implicit conversion function, as long as it is introduced in the written program, will be automatically used by Scala. According to the signature of the implicit conversion function, when using the object defined by the parameter type received by the implicit conversion function in the program, Scala will automatically pass it into the implicit conversion function, convert it to another type of object, and return it. This is "implicit conversion".

It doesn't matter what the implicit conversion function is called, because it's usually not called manually by the user, but by Scala. But if you want to use implicit conversion, you need to import the implicit conversion function. It is therefore generally recommended to name implicit conversion functions of the form "one2one".

There are a large number of implicit conversions and implicit parameters in the Spark source code, so it is necessary to be proficient in this syntax.

implicit conversion

// 要实现隐式转换,只要程序可见的范围内定义隐式转换函数即可。Scala会自动使用隐式转换函数。隐式转换函数与普通函数唯一的语法区别就是,要以implicit开头,而且最好要定义函数返回类型。

// 案例:特殊售票窗口(只接受特殊人群,比如学生、老人等)
class SpecialPerson(val name: String)
class Student(val name: String)
class Older(val name: String)

implicit def object2SpecialPerson (obj: Object): SpecialPerson = {
  if (obj.getClass == classOf[Student]) { val stu = obj.asInstanceOf[Student]; new SpecialPerson(stu.name) }
  else if (obj.getClass == classOf[Older]) { val older = obj.asInstanceOf[Older]; new SpecialPerson(older.name) }
  else Nil
}

var ticketNumber = 0
def buySpecialTicket(p: SpecialPerson) = {
  ticketNumber += 1
  "T-" + ticketNumber
}

Enhance existing types with implicit conversions

// 隐式转换非常强大的一个功能,就是可以在不知不觉中加强现有类型的功能。也就是说,可以为某个类定义一个加强版的类,并定义互相之间的隐式转换,从而让源类在使用加强版的方法时,由Scala自动进行隐式转换为加强类,然后再调用该方法。

// 案例:超人变身

class Man(val name: String)
class Superman(val name: String) {
  def emitLaser = println("emit a laster!")
}

implicit def man2superman(man: Man): Superman = new Superman(man.name)

val leo = new Man("leo")
leo.emitLaser

Implicit conversion of function scope and imports

// Scala默认会使用两种隐式转换,一种是源类型,或者目标类型的伴生对象内的隐式转换函数;一种是当前程序作用域内的可以用唯一标识符表示的隐式转换函数。

// 如果隐式转换函数不在上述两种情况下的话,那么就必须手动使用import语法引入某个包下的隐式转换函数,比如import test._。通常建议,仅仅在需要进行隐式转换的地方,比如某个函数或者方法内,用iimport导入隐式转换函数,这样可以缩小隐式转换函数的作用域,避免不需要的隐式转换。

When the implicit conversion occurs

// 1、调用某个函数,但是给函数传入的参数的类型,与函数定义的接收参数类型不匹配(案例:特殊售票窗口)
// 2、使用某个类型的对象,调用某个方法,而这个方法并不存在于该类型时(案例:超人变身)
// 3、使用某个类型的对象,调用某个方法,虽然该类型有这个方法,但是给方法传入的参数类型,与方法定义的接收参数的类型不匹配(案例:特殊售票窗口加强版)

// 案例:特殊售票窗口加强版
class TicketHouse {
  var ticketNumber = 0
  def buySpecialTicket(p: SpecialPerson) = {
    ticketNumber += 1
    "T-" + ticketNumber
  }
}

Implicit parameter

// 所谓的隐式参数,指的是在函数或者方法中,定义一个用implicit修饰的参数,此时Scala会尝试找到一个指定类型的,用implicit修饰的对象,即隐式值,并注入参数。
// Scala会在两个范围内查找:一种是当前作用域内可见的val或var定义的隐式变量;一种是隐式参数类型的伴生对象内的隐式值

// 案例:考试签到
class SignPen {
  def write(content: String) = println(content)
}
implicit val signPen = new SignPen

def signForExam(name: String) (implicit signPen: SignPen) {
  signPen.write(name + " come to exam in time.")
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325992243&siteId=291194637