Reputation: 19
I try to making token lexical analyzer and I want return Double type using map in scala
var m = Map[String,Double]()
def parseItem(tok: Tokenizer): Double = {
val startPos = tok.tokenPos
val t = tok.token
tok.next()
//(other codes)
else if (t.isIdentifier) {
var t1 = tok.token
if(t1.text == "=") {
tok.next()
var t2 = tok.token
if(t2.isNumber) {
m += (t.text -> t2.number)
println("Idenrifier sorted :" + t.text)
0
}else if(t2.isIdentifier && m.get(t2.text) == None){
println("Error!!! RHS is Wrong identifier!!!")
throw new SyntaxError(startPos, "expected number, identifier, or '('")
}else{
m += (t.text -> m.get(t2.text))
println("Idenrifier sorted :" + t.text)
0
}
}else{
m.get(t.text)
}
the error code is :Option[Double] I think return type is Double But I can't understanding this error
Upvotes: 0
Views: 231
Reputation: 306
m.get(t.text) is of type Option[Double]
You can use the apply method of the Map but keep in mind that the apply method returns the value associated with a given key directly, without wrapping it in an Option. If the key is not defined in the map, an exception is raised.
You can use m(t.text)
or m.apply(t.text)
Or you can use m getOrElse (t.text, defaultValue)
,which returns the value associated with key t.text
in the m
map , or the defaultValue if not found.
Upvotes: 2