3
votes

I am trying to translate some code from haskell to scala language. In haskell I implemented an enum type like that :

data Rank = Jack | Queen | King | Ace | Num Int deriving (Show, Eq) 

I would Like to implement it in scala using selaled case Objects

sealed trait Rank
case object Jack extends Rank
case object Queen extends Rank
case object King extends Rank
case object Ace extends Rank
case object Num Int extends Rank

The problem that for the Num Int type i get an error. I think it should be written as one word ! Any help !

1
@Shoe: I don't think it can count as duplicate of stackoverflow.com/questions/19081904/… when OP's question is not even remotely about type classes. - Régis Jean-Gilles
@RégisJean-Gilles The answers found in that question represent both ADT and type classes in Haskell and Scala form which is what this question is all about. - Shoe
I guess it's debatable, but given how broad the other question is, vs how specific this one is, this does not strike me as being a good candidate for a duplicate. No doubt that the other question is related though, and in any case worth a read for SaKou. - Régis Jean-Gilles

1 Answers

5
votes

In Haskell Num is a class that requires a single type argument, such as Int, to produce a constraint such as Num Int. So in scala you should expect something like that:

case class Num(value: Int) extends Rank

Notice that scala requires you to give the argument a name, unlike haskell

Also you are missing the instances of Show and Eq defined for Rank in scala code, but that doesn't seem to be a part of the question