实例理解scala 隐式转换(隐式值,隐式方法,隐式类)

作用

简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型。
话不多说,直接测试

ImplicitHelper.scala
package com.donews.localspark

/**
  * Created by reynold on 2017/3/20.
  */

object ImplVal {
  implicit val name: String = "reynold"
}

/**
  * 定义成trait的话,可以让ScalaDemo继承,这样就可以自动引入了
  */
 trait ImplVal {
   implicit val name: String = "reynold"
}

object ImplicitHelper {
  /**
    * 隐式参数
    * 隐式参数一般和柯里化进行结合,使用该函数不用给出implicit的值
    *
    * @param param
    * @param impl
    */
  def echo(param: String)(implicit impl: String) {
    println(param + "," + impl)
  }

  /**
    * 隐式转换函数是指在同一个作用域下面,一个给定输入类型并自动转换为指定返回类型的函数,
    * 这个函数和函数名字无关,和入参名字无关,只和入参类型以及返回类型有关
    *
    * @param str
    * @return
    */
  implicit def strToInt(str: String) = str.toInt

  /**
    * 隐式类有如下几个限制:
    * They must be defined inside of another trait/class/object.
    * They may only take one non-implicit argument in their constructor.
    * There may not be any method, member or object in scope with the same name as the implicit class.
    * Note: This means an implicit class cannot be a case class.
    * 隐式类的运作方式:
    * 隐式类的主构造函数只能有一个参数(有两个以上并不会报错,但是这个隐式类永远不会被编译器作为隐式类在隐式转化中使用)
    * 且这个参数的类型就是将要被转换的目标类型
    * 隐式转换类将包裹目标类型,隐式类的所有方法都会自动"附加"到目标类型上
    *
    * @param origin 隐式类构造函数参数
    */
  implicit class ImpAdd(origin: Int) {
    def add(param: Int) = origin + param
  }

}
ScalaDemo.scala
package com.donews.reynold

//import com.donews.localspark.ImplVal
import com.donews.localspark.ImplicitHelper.{ImpAdd, echo, strToInt}
import com.donews.localspark.ImplVal.name

/**
  * Created by reynold on 2017/3/16.
  */
object ScalaDemo /*extends ImplVal*/ {
  def main(args: Array[String]): Unit = {
    println(1.add(2))
    //import com.donews.localspark.ImplicitHelper.strToInt 源码一般放在上面
    println(strToInt("1"))
    println(math.max("1", 2))

    echo("hello")("word")
    //import com.donews.localspark.ImplVal.name
    echo("hello")
    //或者像下面这样
    //implicit val impl = "implicit"
    //echo("hello")
  }
}

结果为:

3
1
2
hello,word
hello,reynold

隐式解析机制

即编译器是如何查找到缺失信息的,解析具有以下两种规则:
1.首先会在当前代码作用域下查找隐式实体(隐式方法  隐式类 隐式对象)
 
2.如果第一条规则查找隐式实体失败,会继续在隐式参数的类型的作用域里查找(如上面代码中的extends部分)
类型的作用域是指与该类型相关联的全部伴生模块,一个隐式实体的类型T它的查找范围如下:
    (1)如果T被定义为T with A with B with C,那么A,B,C都是T的部分,在T的隐式解析过程中,它们的伴生对象都会被搜索
    (2)如果T是参数化类型,那么类型参数和与类型参数相关联的部分都算作T的部分,比如List[String]的隐式搜索会搜索List的
伴生对象和String的伴生对象
    (3) 如果T是一个单例类型p.T,即T是属于某个p对象内,那么这个p对象也会被搜索
    (4) 如果T是个类型注入S#T,那么S和T都会被搜索
 
参考:
http://m.2cto.com/kf/201603/493264.html
https://my.oschina.net/aiguozhe/blog/35968
http://www.cnblogs.com/MOBIN/p/5351900.html
http://blog.csdn.net/oopsoom/article/details/24643869

 

posted @ 2017-03-21 15:16  Reynold.C  Views(2291)  Comments(0Edit  收藏  举报