scala成长之路(3)隐式转换

不废话,先上例子:定义一个参数类型为String的函数:

scala> def sayHello(name:String) = println("hello " + name)
sayHello: (name: String)Unit

然后调用此函数,但传入参数为1,可想而知一定会报错:

scala> sayHello(1)
<console>:13: error: type mismatch;
found : Int(1)
required: String
sayHello(1)

很简单,因为函数要求的参数类型为String,而传入类型为Int,类型不匹配。但是,如果此时定义一个隐式类型转换函数,指明原参数类型和转换后的参数类型,函数名随意╮(╯▽╰)╭:

scala> implicit def hahahaa(s:Int):String = s.toString
<console>:11: warning: implicit conversion method hahahaa should be enabled
by making the implicit value scala.language.implicitConversions visible.
This can be achieved by adding the import clause 'import scala.language.implicitConversions'
or by setting the compiler option -language:implicitConversions.
See the Scaladoc for value scala.language.implicitConversions for a discussion
why the feature should be explicitly enabled.
implicit def hahahaa(s:Int):String = s.toString

虽然报了一大堆警告,但是可以看出来定义成功了。(警告可以通过import scala.language.implicitConversions来消除)

此时再调用

scala> sayHello(1)
hello 1

可以看到调用成功,事实上此类函数时,一般会先根据传入参数类型和要求参数类型寻找相应的隐式转换函数,将转换函数的执行结果作为输入。

回归正题,scala的隐式转换有3中应用场景:

1. 即上边的参数转换场景:当传入的参数与定义的参数类型不匹配,则在当前作用域内寻找对应的隐式转换函数;

2. 参数值隐式传入场景:当函数中有定义隐式参数时(需单独列出来),会在当前作用域寻找同类型的隐式变量/标量,直接传入,无需在调用时传入:

scala> def sayHi(name:String)(implicit words:String) = print("Hi,"+name+"!"+words)
sayHi: (name: String)(implicit words: String)Unit

scala> implicit val w = "nice to meet you!"
w: String = nice to meet you!
scala
> sayHi("zhangsan") Hi,zhangsan!nice to meet you!

其实相当于定制化的函数内变量初始化。需要注意,在这里不能有两个相同类型的隐式值,否则出错。

3. 增强功能型隐式转换:很容易理解,在A类对象上调用B类对象独有方法,肯定报错;但是如果定义了从A类到B类的隐式转换函数,那么久能行通了,先通过转换函数转换后再调用:

ImplicitTest.scala:

package basic{
    package test{
        class Dog(n:String){
            val name = n
            def bark = print("wang,wang,wang...")
            val dogNum = System.nanoTime
        }
        class Person(n:String){
            val name = n
            def talk = print("Hello, nice to meet you.")
        }
    }
}

run.scala

import basic.test._

var liuqing = new Person("Liuqing")
implicit def doBadThings(p:Person) = {
    new Dog(p.name + "_dog")
}
liuqing.bark

执行结果:

D:\workspace\scala\test>scala run.scala
warning: there was one feature warning; re-run with -feature for details
one warning found
wang,wang,wang...

你看,柳青可以学狗叫了^^

但是要注意,这里的隐式转换只在方法调用时能触发,在public成员访问时不能触发,即,这里调用liuqing.dogNum,并不会触发转换,会报错。

隐式转换可以说是scala中的“核武器”,在许多高级编程里都会用到,而且十分实用!下面我们以scala sdk源码为例进行实战分析。

我们知道,在scala中java的容器类是可以直接导入后使用的,然而scala中其实也有与java对应的容器类,而且java的容器类其实有一种便捷的方法转换为scala中对应的容器类:

出自spark:core

import scala.collection.JavaConverters._ //这一句十分重要,没有这个导入就不能用asScala了!!

class SparkConf(loadDefaults: Boolean) extends Cloneable with Logging with Serializable {

 import SparkConf._

private val settings = new ConcurrentHashMap[String, String]()//这里的setting是java.util.concurrent.concurrentMap
...
override def clone: SparkConf = { val cloned = new SparkConf(false) settings.entrySet().asScala.foreach { e => //这里通过asScala瞬间将java.util.HashSet装换为scala.collection.mutable.Set cloned.set(e.getKey(), e.getValue(), true) } cloned }
...

这里用到的是scala自带的将java HashSet转换为scala mutable Set的用法,那么这个神奇的功能究竟是怎么实现的呢?去看看scala sdk的源码:

首先看看JavaConverters究竟是何方神圣

package scala
package collection

import convert._

object JavaConverters extends DecorateAsJava with DecorateAsScala

去掉注释其实内容就这么点。。。是不是傻眼了?继续往父类看:

package scala
package collection
package convert

import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc }
import Decorators._
import scala.language.implicitConversions

/** Defines `asScala` extension methods for [[JavaConverters]]. */
trait DecorateAsScala extends AsScalaConverters {
  /**
   * Adds an `asScala` method that implicitly converts a Java `Iterator` to a Scala `Iterator`.
   * @see [[asScalaIterator]]
   */
  implicit def asScalaIteratorConverter[A](i : ju.Iterator[A]): AsScala[Iterator[A]] =
    new AsScala(asScalaIterator(i))
...
  /**
   * Adds an `asScala` method that implicitly converts a Java `List` to a Scala mutable `Buffer`.
   * @see [[asScalaBuffer]]
   */
  implicit def asScalaBufferConverter[A](l : ju.List[A]): AsScala[mutable.Buffer[A]] =
    new AsScala(asScalaBuffer(l))

  /**
   * Adds an `asScala` method that implicitly converts a Java `Set` to a Scala mutable `Set`.
   * @see [[asScalaSet]]
   */
  implicit def asScalaSetConverter[A](s : ju.Set[A]): AsScala[mutable.Set[A]] =
    new AsScala(asScalaSet(s))

  /**
   * Adds an `asScala` method that implicitly converts a Java `Map` to a Scala mutable `Map`.
   * @see [[mapAsScalaMap]]
   */
  implicit def mapAsScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] =
    new AsScala(mapAsScalaMap(m))
... }

从这里我们大概能看出些眉目 :应该是通过隐式转换的方法将java的每种容器类转换统一转换到AsScala类,那么按照这个推想,asScala方法就有可能是AsScala类中定义的。那么再看看AsScala类中有些什么东东吧:

package scala
package collection
package convert

import java.{ util => ju }

private[collection] object Decorators {
  /** Generic class containing the `asJava` converter method */
  class AsJava[A](op: => A) {
    /** Converts a Scala collection to the corresponding Java collection */
    def asJava: A = op
  }

  /** Generic class containing the `asScala` converter method */
  class AsScala[A](op: => A) {
    /** Converts a Java collection to the corresponding Scala collection */
    def asScala: A = op  //我们用的asScala不就在这么(*^▽^*)
  }

  /** Generic class containing the `asJavaCollection` converter method */
  class AsJavaCollection[A](i: Iterable[A]) {
    /** Converts a Scala `Iterable` to a Java `Collection` */
    def asJavaCollection: ju.Collection[A] = JavaConverters.asJavaCollection(i)
  }

  /** Generic class containing the `asJavaEnumeration` converter method */
  class AsJavaEnumeration[A](i: Iterator[A]) {
    /** Converts a Scala `Iterator` to a Java `Enumeration` */
    def asJavaEnumeration: ju.Enumeration[A] = JavaConverters.asJavaEnumeration(i)
  }

  /** Generic class containing the `asJavaDictionary` converter method */
  class AsJavaDictionary[A, B](m : mutable.Map[A, B]) {
    /** Converts a Scala `Map` to a Java `Dictionary` */
    def asJavaDictionary: ju.Dictionary[A, B] = JavaConverters.asJavaDictionary(m)
  }
}

哈哈,终于找到了,原来你藏在这儿啊!可以看到,这个方法等效于主构造函数传入的参数,而该参数是一个传名调用(关于传名调用和传值调用的区别将在下下节详述),那么再来看看在转换Set时用到的什么样的op吧:

  def asScalaSet[A](s: ju.Set[A]): mutable.Set[A] = s match {
    case null                       => null
    case MutableSetWrapper(wrapped) => wrapped
    case _                          => new JSetWrapper(s)
  }

这里逻辑也很简单,做了个match,很显然,我们的java HashMap既不属于null也不是MutableSetWrapper,所以讲被传给JSetWrapper包装起来,进去看看吧

 case class JSetWrapper[A](underlying: ju.Set[A]) extends mutable.AbstractSet[A] with mutable.Set[A] with mutable.SetLike[A, JSetWrapper[A]] {

    override def size = underlying.size

    def iterator = underlying.iterator

    def contains(elem: A): Boolean = underlying.contains(elem)

    def +=(elem: A): this.type = { underlying add elem; this }
    def -=(elem: A): this.type = { underlying remove elem; this }

    override def add(elem: A): Boolean = underlying add elem
    override def remove(elem: A): Boolean = underlying remove elem
    override def clear() = underlying.clear()

    override def empty = JSetWrapper(new ju.HashSet[A])
    // Note: Clone cannot just call underlying.clone because in Java, only specific collections
    // expose clone methods.  Generically, they're protected.
    override def clone() =
      new JSetWrapper[A](new ju.LinkedHashSet[A](underlying))
  }

代码不算长,注意,它继承了scala.collection.mutable.Set,因此我们可以说这个包装类是一个货真价实的scala Set,它只是把常用的Set接口复写了一遍,实现调用的则是底层java类的api。现在终于明白了吧,所谓“转换”只是将java类的外面套了一层scala的接口而已~

posted @ 2018-08-31 20:51  王亚楼  阅读(364)  评论(0编辑  收藏  举报