SaKou
SaKou

Reputation: 259

Enum type using sealed case objects (from haskell to scala)

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 !

Upvotes: 3

Views: 617

Answers (1)

Archeg
Archeg

Reputation: 8462

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

Upvotes: 5

Related Questions