首页 > 其他 > 详细

scala中的view bound与context bound

时间:2014-12-30 23:16:47      阅读:374      评论:0      收藏:0      [点我收藏+]

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/

scala中的view bound与context bound

原文:http://www.cnblogs.com/goodlucklzq/p/4194749.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!