Currently reading / working my way through "Programming in Scala, First Edition", specifically Chapter 31: Combinator Parsing
The author is describing how to parse a JSON file and offers the following more advanced tranformations:
def obj: Parser[Map[String, Any]] = // Can be improved
"{"~repsep(member, ",")~"}" ^^
{ case "{"~ms~"}" => Map() ++ ms }
later improved to:
def obj: Parser[Map[String, Any]] =
"{"~> repsep(member, ",") <~"}" ^^ (Map() ++ _)
However, when I enter such code into my IDE (IntelliJ IDEA 14.03), the compiler rejects it with:
Expression of type JSON.this.type#Parser[Iterable[Any]] doesn't conform to expected type JSON.this.type#Parser[Map[String,Any]]
I can, of course, make this error go away by changing obj's type to Parser[Iterable[Any]], but this doesn't give the desired result.
What is the correct way to do this?
For whatever it is worth, I'm using jdk 1.7.0_71 and sdk 2.11.5
It depends on the parser for "member". I guess you are using a parser like:
def member: Parser[Any]
Like in the example, try to use the member parser:
def member: Parser[(String, Any)] =
stringLiteral~":"~value ^^
{ case name~":"~value => (name, value) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.