2 ms·
No, your first sentence is wrong. The following definition stores the ranks as unboxed integers, but prints them differently: case class Rank private (val
by fhars 13y ago
No, your first sentence is wrong. The following definition stores the ranks as unboxed integers, but prints them differently:
case class Rank private (val rank : Int) extends AnyVal {
override def toString() = {
rank match {
case 1 => "A"
case 11 => "J"
case 12 => "Q"
case 13 => "K"
case _ => rank.toString()
}
}
}
object Rank {
val Ace = Rank(1)
val Two = Rank(2)
// ...
val King = Rank(13)
}
- drewhk 13y agoAnd you can create a new string interpolation to make card instances, like: card"Heart:Ace" or card"$suite:Ace" if you want to reference a variable.