关于 Scala 界定、隐式转换的一些知识(三)——视图界定

上节举例说明了 上界和 下界(因为没撸点,所以没举例),但有时候这种限定不能满足实际要求,如果希望跨越类继承层次结构时,可以使用视图界定来实现的,其实背后的实现原理是隐式转换(亚麻跌急,这次先露半个腿,我们在下一节细细观摸什么是隐式转换,搜嘎~)来实现。视图界定利用 <% 符号来实现

先看下面的代码:

//使用的是类型变量界定 S <: Comparable[S]

class Student[T, S <: Comparable[S]](var name: T,var height: S)

object ViewBound extends App {
    //可以编译通过,因为 String类型在 Comparable 继承层次体系
    val s = Student("john", "170")

    //下面这条语句不合法,这是因为Int类型没有实现 Comparable 接口
    val s2 = Student("john", 170)
}

val s2 = Student("john", 170) 语句不合法,是因为 Int 类型不属于 Comparable类继承层次结果,
但我们知道 RichInt 类型实现了 Comparable接口,Int 类型可以经过隐式转换成 RichInt 类型,
使用视图界定就可以使 val s2 = Student("john", 170)代码合法,具体代码如下:

//利用<%符号对泛型S进行限定,它的意思是S可以是Comparable类继承层次结构
//中实现了Comparable接口的类也可以是能够经过隐式转换得到的类,该类实现了Comparable接口

case class Student[T, S <% Comparable[S]](var name: T, var height: S)

object ViewBound extends App {
    val s = Student("john", "170")
    //下面这条语句合法,这时 Int 隐式转换 成了 RichInt
    val s2 = Student("john", 170)
}

Int 是如何隐式转换成 RichInt 的呢?而 RihInt 怎样实现的 Comparable 接口的,我们下面分析一下:

查看Scala API文档可以看到 ,Int 类会隐式转换成 RichInt 类,RichInt 并不是直接实现 Comparable口,而是通过ScalaNumberProxy 类将 Comparable 中的方法继承过来:

Int.scala 源码,有这样一段介绍:
There is an implicit conversion from [[scala.Int]] => [[scala.runtime.RichInt]]

但是我没找到 Int 是在哪个类 下面 impliitly 成 RichInt 的,麻痹还藏着掖着不让看。。。

RichInt 继承关系代码:

final class RichInt(val self: Int) extends AnyVal 
        with ScalaNumberProxy[Int] with RangedProxy[Int] {
    ...
}

对应图:


ScalaNumberProxy继承关系代码:

trait ScalaNumberProxy[T] extends Any 
        with ScalaNumericAnyConversions with Typed[T] with OrderedProxy[T] {
    ...
    trait OrderedProxy[T] extends Any with Ordered[T] with Typed[T] {
        protected def ord: Ordering[T]

        def compare(y: T) = ord.compare(self, y)
    }
    ...
}

对应图:


从上面可以看到,ScalaNumberProxy 混入了 OrderedProxy,而 OrderedProxy 又混入了 Ordered,而 Ordered 混入了 Comparable 接口:

trait Ordered[A] extends Comparable[A] {
    ...
}

上面 提到了 Ordered 和 Ordering,这两个长得挺像的是什么鬼?一脸懵逼。。。
没关系,下面这篇文章会介绍给你们认识这对兄弟。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容