I'm completely new to Scala. Right now I'm attempting port a parser I wrote in Standard ML to Scala and having an issue with the following code:
abstract class Token
case class Zero extends Token
case class At extends Token
//...
object Tokenizer {
def tokenize(seq : List[Char]) : List[Token] = seq match {
case List() => error("Empty input")
case '0' :: rest => Zero :: tokenize(rest)
case '@' :: rest => At :: tokenize(rest)
//...
}
}
In SML I wouldn't have to declare the return type of the tokenize() method but it seems Scala needs it and it is somehow not happy with the type I have provided (it complains Zero, At are invalid types and that they should be of type Token instead). Note that I also want to patten match the token list at a later point in time during the parsing phase.
I did some searching on the web and on stackoverflow itself to see if a similar question has been raised before (it looked so trivial) but somehow I couldn't find anything. I'm pretty sure I've got something basic wrong, please feel free to enlighten me :)
At
and Zero
are classes, not objects, so they are not themselves instances of Token
. You can fix your code by changing from case class
to case object
:
case object Zero extends Token
case object At extends Token
The reason why you need to specify the return type of the function is that Scala's compiler can't figure out the type of recursive functions, you can read more about that here: Why does Scala require a return type for recursive functions?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With