I am currently reading through the linked docs and I have the following...
val builder = CsvSchema.builder()
splitString.foreach(col=>builder.addColumn(col));
header = builder.build()
val it: MappingIterator[util.Map[String, String]] =
mapper.readerForListOf(classOf[String])
.with(header)
.readValue(line);
This seems to match the demo code but when I try running I get...
Service.scala:<1369..1373>: an identifier expected, but 'with' found
I also don't see with as an option in the autocomplete. What do I need?
CodePudding user response:
Seem to have gotten it with...
val it: MappingIterator[util.Map[String, String]] =
mapper.readerForListOf(classOf[String])
.`with`(header)
.readValue(line);
CodePudding user response:
with
is a reserved word in Scala. Reserved words are not legal identifiers, see Section 1.1 Identifiers of the Scala Language Specification [bold emphasis mine]:
The following names are reserved words instead of being members of the syntactic class id of lexical identifiers.
Identifiers can also be formed by [bold emphasis mine]:
Finally, an identifier may also be formed by an arbitrary string between back-quotes (host systems may impose some restrictions on which strings are legal for identifiers). The identifier then is composed of all characters excluding the backquotes themselves.
Note the phrase "arbitrary string" without any restriction. This means, it can be a reserved word as well.
The exact problem you are having is actually explicitly addressed:
When one needs to access Java identifiers that are reserved words in Scala, use backquote-enclosed strings. For instance, the statement
Thread.yield()
is illegal, sinceyield
is a reserved word in Scala. However, here's a work-around:Thread.`yield`()
As a sidenote: you should try using a different IDE or editor. Even the extremely simplistic, crappy syntax highlighting on Stack Overflow clearly shows the problem right there in your question: with
is highlighted as a reserved word, the same way as val
. A good IDE or editor should do the same and immediately show you that with
is a reserved word and not a legal identifier.