本节主要内容
- 隐式参数中的隐式转换
- 函数中隐式参数使用概要
- 隐式转换问题梳理
1. 隐式参数中的隐式转换
前一讲中,我们提到函数中如果存在隐式参数,在使用该函数的时候如果不给定对应的参数,则编译器会自动帮我们搜索相应的隐式值,并将该隐式值作为函数的参数,这里面其实没有涉及到隐式转换,本节将演示如何利用隐式参数进行隐式转换,下面的代码给定的是一个普通的比较函数:
object ImplicitParameter extends App {
def compare[T](first:T,second:T)={
if (first < second)
first
else
second
}
}
上面的代码要想使其编译通过,可以前类型变量界定和视图界定指定其上界为Ordered[T],例如:
object ImplicitParameter extends App {
def compare[T<:Ordered[T]](first:T,second:T)={
if (first < second)
first
else
second
}
}
这是一种解决方案,我们还有一种解决方案就是通过隐式参数的隐式转换来实现,代码如下:
object ImplicitParameter extends App {
def compare[T](first:T,second:T)(implicit order:T=>Ordered[T])={
if (first > second)
first
else
second
}
println(compare("A","B"))
}
2. 函数中隐式参数使用概要
要点1:在定义函数时,如果函数没有柯里化,implicit关键字会作用于所有参数,例如:
def sum(implicit x: Int, y: Int) = x + y
另外,值得注意的是,def maxFunc(implicit x: Int, y: Int) = x + y
在使用时,也只能指定一个隐式值,即指定的隐式值分别会对应函数中的参数(这里是x,y),代码如下:
def sum(implicit x: Int, y: Int) = x + y
implicit val x:Int=3
println(sum)
要点2:要想使用implicit只作用于某个函数参数,则需要将函数进行柯里化,如:
def sum(x: Int)(implicit y:Int)=x+y
值得注意的是,下面这种两种带隐式参数的函数也是不合法的
def sum(x: Int)(implicit y:Int)(d:Int)=x+y+d
def sum(x: Int)(implicit y:Int)(implicit d:Int)=x+y+d
要点3: 匿名函数不能使用隐式参数,例如:
val sum2=(implicit x:Int)=>x+1
要点4: 如何函数带有隐式参数,则不能使用其偏函数,例如:
def sum(x: Int)(implicit y:Int)=x+y
def sum2=sum _
3. 隐式转换问题梳理
1 多次隐式转换问题
在上一讲中我们提到,隐式转换从源类型到目标类型不会多次进行,也即源类型到目标类型的转换只会进行一次
class RichFile(val file:File){
def read=Source.fromFile(file).getLines().mkString
}
class RichFileAnother(val file:RichFile){
def read2=file.read
}
val f=new File("file.log").read2
println(f)
注意这里指的是源类型到目标类型的转换只会进行一次,并不是说不存在多次隐式转换,在一般的方法调用过程中可能会出现多次隐式转换,例如:
class ClassA {
override def toString() = "This is Class A"
}
class ClassB {
override def toString() = "This is Class B"
}
class ClassC {
override def toString() = "This is ClassC"
def printC(c: ClassC) = println(c)
}
class ClassD
object ImplicitWhole extends App {
implicit def B2C(b: ClassB) = {
println("B2C")
new ClassC
}
implicit def D2C(d: ClassD) = {
println("D2C")
new ClassC
}
new ClassD().printC(new ClassB)
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
还有一种情况也会发生多次隐式转换,如果给函数定义了隐式参数,在实际执行过程中可能会发生多次隐式转换,代码如下:
object Main extends App {
class PrintOps() {
def print(implicit i: Int) = println(i);
}
implicit def str2PrintOps(s: String) = {
println("str2PrintOps")
new PrintOps
}
implicit def str2int(implicit s: String): Int = {
println("str2int")
Integer.parseInt(s)
}
implicit def getString = {
println("getString")
"123"
}
"a".print
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
上面这个例子来源于:爱国者的博客,感谢该作者的无私奉献
2 要不要用隐式转换的问题
从上述代码中可以看到,隐式转换功能很强大,但同时也带来了程序复杂性性问题,在一个程序中如果大量运用隐式转换,特别是涉及到多次隐式转换时,会使代码理解起来变得比较困难,那到底要不要用隐式转换呢?下面给出我自己开发实践中的部分总结,供大家参考:
1 即使你能轻松驾驭scala语言中的隐式转换,能不用隐式转换就尽量不用
2 如果一定要用,在涉及多次隐式转换时,必须要说服自己这样做的合理性
3 如果只是炫耀自己的scala语言能力,请大胆使用