2013-05-17 39 views
-1

我有以下代码:麻烦与scala.util.parsing.combinator.ImplicitConversions

import scala.util.parsing.combinator._ 
import scala.language.implicitConversions 

object Parser1 extends RegexParsers with ImplicitConversions with PackratParsers { 
    lazy val e: PackratParser[Int] = (
     e ~ "+" ~ e ^^ { (e1, _, e2) => e1 + e2 } 
    | e ~ "-" ~ e ^^ { (e1, _, e2) => e1 - e2 } 
    | """\d+""".r ^^ { _.toInt } 
) 
} 

不编译:

error: wrong number of parameters; expected = 1 
     e ~ "+" ~ e ^^ { (e1, _, e2) => e1 + e2 } 
           ^

e定义从Scala Style Guide服用。我希望(和预期)发生的是自动使用隐含转换flatten3ImplicitConversions。它的工作原理,如果我手动添加:

object Parser1 extends RegexParsers with ImplicitConversions with PackratParsers { 
    lazy val e: PackratParser[Int] = (
     e ~ "+" ~ e ^^ flatten3({ (e1, _, e2) => e1 + e2 }) 
    | e ~ "-" ~ e ^^ flatten3({ (e1, _, e2) => e1 - e2 }) 
    | """\d+""".r ^^ { _.toInt } 
) 
} 

我知道它的范围,有正确的类型,和工作,并宣布在斯卡拉源隐含的,所以为什么不编译器使用隐式转换?

回答

1

有权型

尝试增加参数类型:

{ (e1: Int, _: Any, e2: Int) => e1 + e2 }

object Parser1 extends RegexParsers with ImplicitConversions with PackratParsers { 
    lazy val e: PackratParser[Int] = (
     e ~ "+" ~ e ^^ { (e1: Int, _: Any, e2: Int) => e1 + e2 } 
    | e ~ "-" ~ e ^^ { (e1: Int, _: Any, e2: Int) => e1 - e2 } 
    | """\d+""".r ^^ { _.toInt } 
) 
} 
+0

感谢SOM-snytt,这确实工作。我仍然不明白为什么编译器不能自己解决这个问题。 – wingedsubmariner