我已經寫了下面的代碼部分來解析表達式中的RDD類型和Float。解析算術表達式由浮子和RDD喜歡:「firstRDD + 2」:如何解析Scala中一個解析器中的兩種不同類型?
def term2: Parser[List[Either[Float, RDD[(Int,Array[Float])]]]] = rep(factor2)
def factor2: Parser[Either[Float, RDD[(Int,Array[Float])]]] = pathxml | num
def pathxml: Parser[RDD[(Int,Array[Float])]] = pathIdent ^^ {s => pathToRDD(s) } //pathToRDD is a function that gets the path in string and create an RDD from the file inside that path and pathIdent parse to see whether the input string is a path or not
def num: Parser[Float] = floatingPointNumber ^^ (_.toFloat)
現在我收到此錯誤:
[error] type mismatch;
[error] found : ParseExp.this.Parser[Float]
[error] required: ParseExp.this.Parser[Either[Float,org.apache.spark.rdd.RDD[(Int, Array[Float])]]]
[error] def factor2: Parser[Either[Float, RDD[(Int,Array[Float])]]] = pathxml | num
[error] ^
我不知道該怎麼做,但使用「要麼「而且不知道如何解決這種類型的不匹配問題! 請注意,如果我使用「任何」,它無法解析RDD。
你在那裏使用的Parser類是什麼?我找不到相應的API文檔。 – Reactormonk
也許你可以使用http://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/中定義的聯合類型。 https://raw.githubusercontent.com/zalacer/projects-tn/master/UnionTypeDemo/src/tn/UnionTypeDemo.scala中有一個簡單的工作示例。 – 2015-07-12 01:11:01
@TrisNefzger這是正確的方式來做到這一點,我同意。但是Rubbic似乎並不太熟悉scala的美麗的一面,所以我不想過頭。 – Reactormonk