scala中的view bound与context bound

1.scala中的<%意识是“view bounds”(视界) ,它比<:的使用范围更广,还能进行隐式转换,是一种语法糖。

下面的两种写法是等效的,在编译之后完全一样。

object Test {

  def main(args: Array[String]) {

  }

  def method1[A<% Int](a:Int): Unit ={

  }

  def method2[A](a:Int)(implicit b:A=>Int): Unit ={

  }

}
View Code

其中的Function1的定义如下

2.与view bounds一样context bounds(上下文界定)也是隐式参数的语法糖

object Test {

  def main(args: Array[String]) {

  }

  def method1[A:List](a:A): Unit ={

  }

  def method2[A](a:A)(implicit b:List[A]): Unit ={

  }

}

参考:

       1.http://www.scala-lang.org/api/2.10.4/#scala.Function1

       2.http://hongjiang.info/scala-type-system-view-bounds/

       3.http://hongjiang.info/scala-type-system-context-bounds/

原文地址:https://www.cnblogs.com/goodlucklzq/p/4194749.html