类扩展与隐式转换
在Scala中,可以通过隐式转换来扩展类的功能,例如对String类进行扩展以提供更多的操作方法。例如,下面的代码展示了如何检查一个字符串是否包含数字:
"+9519760513".exists(_.isDigit) 尽管java.lang.String本身没有exists方法,但Scala的标准库通过在Predef对象中定义了一个隐式转换,将String隐式转换为StringOps,从而提供了这一方法。
object Predef { implicit def augmentString(x: String): StringOps = new StringOps(x) } 隐式解析规则
标记规则
只有被标记为
implicit的定义才可用作隐式转换。
例如,intWrapper方法将Int类型包装成RichInt类型:
object Predef { implicit def intWrapper(x: Int) = new scala.runtime.RichInt(x) } 另一个例子是在Predef中定义的any2stringadd类,它允许任何类型的对象与字符串进行拼接:
object Predef { implicit final class any2stringadd[A](private val self: A) extends AnyVal { def +(other: String): String = String.valueOf(self) + other } } 作用域规则
插入的隐式转换必须在作用域内作为一个单一标识符存在,或者与转换的源类型或目标类型相关联。
例如,定义了两个案例类Yard和Mile:
case class Yard(val amount: Int) case class Mile(val amount: Int) 可以在object Mile中定义一个隐式转换mile2yard,将Mile转换为Yard:
object Mile { implicit def mile2yard(mile: Mile) = new Yard(10 * mile.amount) } 同样,这个转换也可以在object Yard中定义:
object Yard { implicit def mile2yard(mile: Mile) = new Yard(10 * mile.amount) } 在需要将Mile转换为Yard时,通常会遇到以下两种情况:
传递参数时,但类型不匹配;
赋值表达式中,但类型不匹配。
例如:
def accept(yard: Yard) = println(yard.amount + " yards") accept(Mile(10)) val yard: Yard = Mile(10) 其他规则
一次一条规则:每次只尝试一个隐式转换。
显式优先规则:如果代码本身类型检查通过,则不会尝试隐式转换。
无歧义规则:只有当没有其他可能的转换时,才会插入隐式转换。
隐式转换的尝试位置
转换为预期类型
传递参数时,但类型不匹配;
赋值表达式中,但类型不匹配。
接收者的选择转换
调用方法,但方法不存在;
调用方法,方法存在但参数类型不匹配。
隐式参数
隐式参数
隐式参数允许方法在调用时自动提供某些参数,这些参数通常由编译器根据上下文自动推断。例如:
import scala.math.Ordering case class Pair[T](first: T, second: T) { def smaller(implicit order: Ordering[T]) = order.min(first, second) } 当T为Int时:
Pair(1, 2).smaller 实际上编译器调用的是:
Pair(1, 2).smaller(Ordering.Int) 其中Ordering.Int定义在Ordering的伴生对象中:
object Ordering { trait IntOrdering extends Ordering[Int] { def compare(x: Int, y: Int) = if (x 因此:
implicitly[Ordering[Int]] == Ordering.Int // true 其中,implicitly是定义在Predef中的一个工具函数,用于提取隐式值:
@inline def implicitly[T](implicit e: T) = e 对于自定义类型,也可以类似地定义隐式参数:
import scala.math.Ordering case class Point(x: Int, y: Int) object Point { implicit object OrderingPoint extends Ordering[Point] { def compare(lhs: Point, rhs: Point): Int = (lhs.x + lhs.y) - (rhs.x + rhs.y) } } Pair(Point(0, 0), Point(1, 1)).smaller 等价于:
Pair(Point(0, 0), Point(1, 1)).smaller(Point.OrderingPoint) 因此:
implicitly[Ordering[Point]] == Point.OrderingPoint 上下文绑定
上下文绑定(Context Bound)是一种简化的语法,用于声明一个类型参数必须有一个特定类型的隐式值。例如:
import scala.math.Ordering case class Pair[T : Ordering](first: T, second: T) { def smaller(implicit order: Ordering[T]) = order.min(first, second) } 可以进一步简化为:
import scala.math.Ordering case class Pair[T : Ordering](first: T, second: T) { def smaller = implicitly[Ordering[T]].min(first, second) } 甚至可以更简洁地表示为:
import scala.math.Ordering case class Pair[T : Ordering](first: T, second: T) { def smaller = Ordering[T].min(first, second) } 这里,Ordering[T]实际上是调用了object Ordering的apply方法,从而便捷地找到了Ordering[T]的隐式值:
object Ordering { def apply[T](implicit ord: Ordering[T]) = ord } 因此,Ordering[T].min等价于implicitly[Ordering[T]].min。
视图绑定
视图绑定(View Bound)允许一个类型参数可以隐式转换为另一个类型。例如:
import scala.math.Ordered case class Pair[T](first: T, second: T) { def smaller(implicit order: T => Ordered[T]) = { if (order(first) 在这里,order既是隐式参数,也是一个隐式转换函数。例如:
Pair(1, 2).smaller 等价于:
Pair(1, 2).smaller(Predef.intWrapper _) 这种设计使得隐式参数order变得多余,形成了一个常见的模式——视图绑定:
import scala.math.Ordered case class Pair[T <% Ordered[T]](first: T, second: T) { def smaller = if (first 需要注意的是,T <% Ordered[T]表示T可以隐式转换为Ordered[T],而T <: Ordered[T]表示T是Ordered[T]的子类型。
上限约束
上限约束(Upper Bound)用于限制类型参数必须是某个特定类型的子类型。例如:
import scala.math.Ordered case class Pair[T <: Comparable[T]](first: T, second: T) { def smaller = if (first.compareTo(second) <0) first else second } 在这个例子中:
Pair("1", "2").smaller // OK, String is subtype of Comparable[String] Pair(1, 2).smaller // Compile Error, Int is not subtype of Comparable[Int] 



京公网安备 11010802041100号