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 🙂
If you want to create new instances of
ZeroandAtcase classes, then you should useapplyfactory method to instantiate them (ornewkeyword:new Zero), like this (in ScalaZero()would be equal toZero.apply()):If you write just
Zero(and notZero()) then you are using companion object ofZeroclass, that was created automatically by compiler.