2013-05-17 229 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