scala implicit 隐式转换

来源:互联网 发布:java字符串长度函数 编辑:程序博客网 时间:2024/05/17 06:47

我们经常在scala 源码里上看到类似implicit这个关键字。

一开始不太理解,后来看了看,发现implicit这个隐式转换是支撑scala的易用、容错以及灵活语法的基础。

我们经常使用的一些集合方法里面就用到了implicit,比如:

[java] view plain copy
  1. def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {  

1. 隐式转换函数的定义:

我们在scala repl里定义一个方法foo,接受一个string的参数,打印出message

 当我们传入字符串参数"2"的时候,输出2

 但是当传入的类型是int而不是string的时候,出现类型不匹配异常。

 如果我们想支持隐式转换,将int转化为string,可以定义一个隐式函数,def implicit intToString( i : Int) = i.toString

 这里注意一下这个隐式函数的输入参数和返回值。

 输入参数:接受隐式转换入参为int类型

 返回值: 返回结果是string.

[java] view plain copy
  1. scala> def foo(msg : String) = println(msg)  
  2. foo: (msg: String)Unit  
  3.   
  4. scala> foo("2")  
  5. 2  
  6.   
  7. scala> foo(3)  
  8. <console>:9: error: type mismatch;  
  9.  found   : Int(3)  
  10.  required: String  
  11.               foo(3)  
  12.                   ^  
  13.   
  14. scala> implicit def intToString(i:Int) = i.toString  
  15. warning: there were 1 feature warning(s); re-run with -feature for details  
  16. intToString: (i: Int)String  
  17.   
  18. scala> foo(3)  
  19. 3  
[java] view plain copy
  1. scala> def bar(msg : String) = println("aslo can use implicit function intToString...result is "+msg)  
  2. bar: (msg: String)Unit  
[java] view plain copy
  1. scala> bar(33)  
  2. aslo can use implicit function intToString...result is 33  

总结一下,我的理解是:隐式函数是在一个scop下面,给定一种输入参数类型,自动转换为返回值类型的函数,和函数名,参数名无关。

这里intToString隐式函数是作用于一个scop的,这个scop在当前是一个scala repl,超出这个作用域,将不会起到隐式转换的效果。


为什么我会这么定义隐式函数,下面我再定义一个相同的输入类型为int,和返回值类型为string的隐式函数名为int2str:

[java] view plain copy
  1. scala> implicit def int2str(o :Int) = o.toString  
  2. warning: there were 1 feature warning(s); re-run with -feature for details  
  3. int2str: (o: Int)String  
  4.   
  5. scala> bar(33)  
  6. <console>:13: error: type mismatch;  
  7.  found   : Int(33)  
  8.  required: String  
  9. Note that implicit conversions are not applicable because they are ambiguous:  
  10.  both method intToString of type (i: Int)String  
  11.  and method int2str of type (o: Int)String  
  12.  are possible conversion functions from Int(33) to String  
  13.               bar(33)  
  14.                   ^  

跑出了二义性的异常,说的是intToString和int2str这2个隐式函数都是可以处理bar(33)的,编译器不知道选择哪个了,呵呵。。

证明了隐式函数和函数名,参数名无关,只和输入参数与返回值有关。


2. 隐式函数的应用

我们可以随便的打开scala函数的一些内置定义,比如我们最常用的map函数中->符号,看起来很像php等语言。

但实际上->确实是一个ArrowAssoc类的方法,它位于scala源码中的Predef.scala中。下面是这个类的定义:

[java] view plain copy
  1. final class ArrowAssoc[A](val __leftOfArrow: A) extends AnyVal {  
  2.   // `__leftOfArrow` must be a public val to allow inlining. The val  
  3.   // used to be called `x`, but now goes by `__leftOfArrow`, as that  
  4.   // reduces the chances of a user's writing `foo.__leftOfArrow` and  
  5.   // being confused why they get an ambiguous implicit conversion  
  6.   // error. (`foo.x` used to produce this error since both  
  7.   // any2Ensuring and any2ArrowAssoc pimped an `x` onto everything)  
  8.   @deprecated("Use `__leftOfArrow` instead""2.10.0")  
  9.   def x = __leftOfArrow  
  10.   
  11.   @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(__leftOfArrow, y)  
  12.   def →[B](y: B): Tuple2[A, B] = ->(y)  
  13. }  
  14. @inline implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x)  

我们看到def ->[B] (y :B)返回的其实是一个Tuple2[A,B]类型。

我们定义一个Map:

[java] view plain copy
  1. scala> val mp = Map(1->"game1",2->"game_2")  
  2. mp: scala.collection.immutable.Map[Int,String] = Map(1 -> game1, 2 -> game_2)  

这里 1->"game1"其实是1.->("game_1")的简写。

这里怎么能让整数类型1能有->方法呢。

这里其实any2ArrowAssoc隐式函数起作用了,这里接受的参数[A]是泛型的,所以int也不例外。

调用的是:将整型的1 implicit转换为 ArrowAssoc(1)

看下构造方法,将1当作__leftOfArrow传入。

->方法的真正实现是生产一个Tuple2类型的对象(__leftOfArrow,y ) 等价于(1, "game_id")

这就是一个典型的隐式转换应用。


其它还有很多类似的隐式转换,都在Predef.scala中:

例如:Int,Long,Double都是AnyVal的子类,这三个类型之间没有继承的关系,不能直接相互转换。

在Java里,我们声明Long的时候要在末尾加上一个L,来声明它是long。

但在scala里,我们不需要考虑那么多,只需要:

[java] view plain copy
  1. scala> val l:Long = 10  
  2. l: Long = 10  

这就是implicit函数做到的,这也是scala类型推断的一部分,灵活,简洁。

其实这里调用是:

val l : Long = int2long(10)


最后的总结:

1. 记住隐式转换函数的同一个scop中不能存在参数和返回值完全相同的2个implicit函数。

2. 隐式转换函数只在意 输入类型,返回类型。

3. 隐式转换是scala的语法灵活和简洁的重要组成部分。

0 0