使用类型注释函数文本的隐式参数

时间:2011-04-04 14:41:24

标签: function scala literals implicit type-parameter

如果我希望该参数是隐式的,如何使用类型注释函数文字的参数?

trait Test { def call[C, T](fun: C => T): T }

def test1(t: Test): Unit =
  t.call { c: Int => 33 } // ok

def test2(t: Test): Unit =
  t.call { implicit c => 33 } // type missing...

def test3(t: Test): Unit =
  t.call { (implicit c: Int) => 33 } // not possible...

1 个答案:

答案 0 :(得分:2)

Scala 2.8.0引入了函数文字的隐式参数。但是,正如您所发现的那样,语法仅限于没有类型注释的单个参数。

可以在Scala 2.9.0中使用单个类型参数。

https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/pos/t3672.scala?rev=23288

https://lampsvn.epfl.ch/trac/scala/changeset/23289

但IntelliJ Parser尚未处理它们:http://youtrack.jetbrains.net/issue/SCL-2441