Question

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 :)

Was it helpful?

Solution

If you want to create new instances of Zero and At case classes, then you should use apply factory method to instantiate them (or new keyword: new Zero), like this (in Scala Zero() would be equal to Zero.apply()):

case '0' :: rest => Zero() :: tokenize(rest)

If you write just Zero (and not Zero()) then you are using companion object of Zero class, that was created automatically by compiler.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top