【发布时间】:2015-07-11 22:18:38
【问题描述】:
我编写了以下部分代码来解析表达式中的 RDD 类型和浮点数。解析一个由 float 和 RDD 组成的算术表达式,例如:“firstRDD + 2”:
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] ^
除了使用“Either”之外,我不知道该怎么做,也不知道如何解决这种类型不匹配! 请注意,如果我使用“Any”,则无法解析 RDD。
【问题讨论】:
-
你在那里使用的
Parser类是什么?我找不到对应的 API 文档。 -
也许您可以使用milessabin.com/blog/2011/06/09/scala-union-types-curry-howard 中定义的联合类型。 raw.githubusercontent.com/zalacer/projects-tn/master/… 有一个简单的工作示例。
-
@TrisNefzger 这是正确的做法,我同意。但 Rubbic 似乎对 scala 的美丽方面不太熟悉,所以我不想做得过火。
-
是的,我是 Scala 的新手,作为初学者编写一个庞大的程序就像是一种痛苦!谢谢你帮助我!
标签: scala parsing rdd type-mismatch