scala隐式参数(implicit)

来源:互联网 发布:mac os x 10.7.4 编辑:程序博客网 时间:2024/05/17 02:38
SBinary 和 Scalacheck的一部分库,广泛使用了隐式参数。让人联想起了Haskell(一种函数式编程语言)的编程风格,我真心的希望在Scala中这种编程风格更加广泛。这是一种非常有用的技术。作为开始如下多重入参(柯里化)的例子:

scala -> def foo(x: Int)(y: Int) = x+y

foo: (x: Int)(y: Int) Int

 

scala -> foo (1) (2)

ret0:Int = 3

scala> foo(1, 2);:6: error: wrong number of arguments for method foo: (Int)(Int)Int       foo(1, 2);       ^scala> foo(1):6: error: missing arguments for method foo in object $iw;follow this method with `_' if you want to treat it as a partially applied function       foo(1)上面你完全可以用单一函数的入参列表来调用它,但是你必须用不同的语法来调用它(两个挎号)有一种方式你可以申明最后一个参数为隐式(implicit),不需要显式的传递。语法如下:
scala> def speakImplicitly (implicit greeting : String) = println(greeting)speakImplicitly: (implicit String)Unitscala> speakImplicitly("Goodbye world")Goodbye worldscala> speakImplicitly:6: error: no implicit argument matching parameter type String was foud.scala> implicit val hello = "Hello world"hello: java.lang.String = Hello worldscala> speakImplicitlyHello world你可以正常调用,也可以不传参数,系统将从作用域中找到一个被标示为
implicit变量,如果找不到implicit变量,编译的时候将会报错。隐式变量的查找是强制类型匹配的,下面是一些例子展现:
错误的类型:

 

scala> def speakImplicitly (implicit greeting : String) = println(greeting)speakImplicitly: (implicit String)Unitscala> implicit val aUnit = ();aUnit: Unit = ()scala> speakImplicitly:7: error: no implicit argument matching parameter type String was found.只有String类型的隐式变量才能够匹配。错误的泛型:
scala> def speakImplicitly (implicit greeting : String) = println(greeting)speakImplicitly: (implicit String)Unitscala> implicit val hello : Any = "Hello world"hello: Any = Hello worldscala> speakImplicitly:7: error: no implicit argument matching parameter type String was found.隐式参数匹配必须是静态类型,泛型不能够匹配。多个相同类型的参数:
scala> def speakImplicitly (implicit greeting : String) = println(greeting)speakImplicitly: (implicit String)Unitscala> implicit val foo = "foo";foo: java.lang.String = fooscala> implicit val bar = "bar";bar: java.lang.String = barscala> speakImplicitly:9: error: ambiguous implicit values: both value bar in object $iw of type => java.lang.String and value foo in object $iw of type => java.lang.String match expected type String系统找到两个隐式参数是相同的类型,将无法识别。子类可以匹配,
scala> def sayThings (implicit args : List[Any]) = args.foreach(println(_))sayThings: (implicit List[Any])Unitscala> implicit val nothingNiceToSay : List[Any] = NilnothingNiceToSay: List[Any] = List()scala> sayThingsscala> implicit val hellos : List[String] = List("Hello world");hellos: List[String] = List(Hello world)scala> sayThingsHello worldString作为Any的子类,所以系统可以匹配到.

 原地址:http://criss.iteye.com/blog/1806515

0 0
原创粉丝点击