I am parsing a series of XML responses from a external data store. During which I must test for the existence of a child node and – if it exists – test its value. To achieve that I have the following code:
...
val properties = for {
val row <- root \\ "ResultDescription"
val cond:Boolean = checkDetectionNode(row) match {
case Some(nodeseq) => {
val txt = nodeseq.text.toLowerCase
if (txt contains "non-detect")
false
else
true
}
case None => true
}
if (cond)
val name = (row \ "CharacteristicName").text
if (charNameList.exists(s => s == name) == false)
} yield {
getObservedProperty(name) match {
case Some(property) => {
charNameList = name :: charNameList
property
}
}
}
...
checkDetectionNode is defined as such:
private def checkDetectionNode(row: scala.xml.NodeSeq) : Option[scala.xml.NodeSeq] = {
if ((row \ "ResultDetectionConditionText") != null)
Some[scala.xml.NodeSeq]((row \ "ResultDetectionConditionText"))
else
None
}
The above code results in an unspecified error of “illegal start of simple expression” on the val name... line. To be honest I am not a Scala programmer or even a functional programmer (always was more partial to OO/imperative). I’ve only been using Scala for a few days and been basing most of what I know from Java and lambda operators. Unfortunately, I don’t really have the time to sit down and really learn Scala like I wish I could. Deadlines, make fools of us all.
I am hoping that someone could take a look and let me know if there is something I am doing wrong (as I am sure there is). I tried to limit the code shown to, what I hope, is relevant to the question. However, please let me know if any additional code is needed.
Thanks
The xml is a distraction here. The problem is the if (cond) at the end is not acting as a guard, it just looks like it should, the compiler thinks is the start of a new if ‘then’ part.
In the following example:
you’ll get List(6,8,10) as you might expect.
Using
should get you a deprecation warning, and
gets
Simply remove the val in front of your Generator (Pattern1 ‘<-’ Expr [Guard]), and you can resume normal service. It also flows a bit more nicely without the vals in the for loop I find.