Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala case classes and lists

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

like image 624
Asiri Rathnayake Avatar asked Dec 04 '22 07:12

Asiri Rathnayake


1 Answers

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?

like image 54
Theo Avatar answered Jan 03 '23 14:01

Theo