scala类型系统:13) context bounds

view bounds一样context bounds(上下文界定)也是隐式参数的语法糖。context bounds的出现是scala2.8版本才增加的,起因是2.8版本修正了数组类型的设计(关于这个话题后续单独讨论),同时为语法上的方便,引入了”上下文界定”这个概念。

看一个简单的例子,对于比较两个数的大小,采用隐式参数的做法:

scala> def max[T](a:T, b:T) (implicit cp : Comparator[T]) = { 
            if (cp.compareTo(b) > 0) a else b 
        }

可以简化为:

scala> def max[T : Comparator] (a:T, b:T) = { … }

上面的类型参数声明 T : Comparator 就表示存在一个 Comparator[T]类型的隐式值。

等等,原max方法里是要用到 cp 这个隐式参数的,省略后怎么用这个参数呢?有两个途径:

第一种是,在内部定义函数并声明隐式参数

scala> def max[T : Comparator] (a:T, b:T) = { 

            def inner(implicit c:Comparator[T]) = c.compare(a,b);  

            if(inner>0) a else b 
        }

这种做法只是把外部方法的隐式参数隐藏了,放到内部嵌套函数上;还有一个推荐的做法,使用implicitly方法:

scala> def max2[T : Comparator] (a:T, b:T) = { 

            val cp = implicitly[Comparator[T]]

            if( cp.compare(a,b)>0) a else b 
        }

implicitly是在Predef.scala里定义的,它是一个特殊的方法,编译器会记录当前上下文里的隐式值,而这个方法则可以获得某种类型的隐式值。

如果上下文中存在一个Comparator[Int]类型的隐式值:

scala> implicit val c = new Comparator[Int]{ 
                override def compare(a:Int, b:Int) = a - b 
        }

那么对maxmax2都可以对Int型的参数正确执行:

scala> max(1,2)
res4: Int = 2

scala> max2(3,2)
res5: Int = 3

scala类型系统:13) context bounds》上有11个想法

  1. 豁然开朗,看finagle的一段代码,看到implicitly用法,看不懂了,看到这,有些懂了,学习了,多谢


  2. 看一个简单的例子,对于比较两个数的大小,采用隐式参数的做法:

    scala> def max[T](a:T, b:T) (implicit cp : Comparator[T]) = {
    if (cp.compareTo(b) > 0) a else b
    }


    应该是

    def max[T](a:T, b:T) (implicit cp : Comparator[T]) = {
    if (cp.compare(a, b) > 0) a else b
    }

  3. Pingback引用通告: scala雾中风景(14): trait的泛型参数为何不支持context bounds | 在路上

  4. gardenia:tmp hary$ scala -version
    Scala code runner version 2.11.2 — Copyright 2002-2013, LAMP/EPFL
    gardenia:tmp hary$ scala
    Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_60).
    Type in expressions to have them evaluated.
    Type :help for more information.

    scala> def max[T](a:T, b:T) (implicit cp : Comparator[T]) = {
    | if (cp.compareTo(b) > 0) a else b
    | }
    :7: error: not found: type Comparator
    def max[T](a:T, b:T) (implicit cp : Comparator[T]) = {
    ^

    scala>

    • scala> import java.util._
      import java.util._

      scala> def max[T](a:T, b:T) (implicit cp : Comparator[T]) = { if (cp.compare(a, b) > 0) a else b }
      max: [T](a: T, b: T)(implicit cp: java.util.Comparator[T])T

  5. http://stackoverflow.com/questions/3855595/what-is-the-scala-identifier-implicitly
    在这个问题里,有这个一句话:
    What if we want to change the way that the name is output? We can explicitly call PersonShow, explicitly pass an alternative Show[String], but we want the compiler to pass the Show[Int].

    Person.PersonShow(si = implicitly, ss = Show.ShoutyStringShow).show(p)

    其中:si = implicitly ,si怎么是Show[Int],implicitly不带参数,怎么理解

发表评论

电子邮件地址不会被公开。 必填项已用*标注