After creating a ScalaIDE Worksheet named test.WsTemp
, I wrote the code below and am receiving three errors for a single line in trait Enum
:
- Diverging implicit expansion for type
scala.math.Ordering[U]
starting with method ordered in traitLowPriorityOrderingImplicits
- No implicit
Ordering
defined forU
- Not enough arguments for method sorted:
(implicit ord: scala.math.Ordering[U])List[U]
, Unspecified value parameterord
.
Why isn't this working since it's obvious Val
extends Ordered[Val]
?
object WsTemp {
trait Val extends Ordered[Val] {
val id: Int
val name: String
final def compare(that: Val) = this.id - that.id
override def toString: String = name
}
trait Enum[U <: Val] {
protected def init: Set[U]
val all = init
val allOrdered = all.toList.sorted // <-- Receiving error here
val byId = all.map(x => (x.id, x)).toMap
val byName = all.map(x => (x.name, x)).toMap
def apply(id: Int) = byId.get(id)
def apply(name: String) = byName.get(name)
}
sealed class Value(val id: Int, val name: String) extends Val
object Suit extends Enum[Value] {
override def init: Set[Value] = //Set()
Set(
new Value(0, "Spade")
, new Value(1, "Club")
, new Value(2, "Diamond")
, new Value(3, "Heart")
)
val Spade = Suit.byId(0)
val Club = Suit.byId(1)
val Diamond = Suit.byId(2)
val Heart = Suit.byId(3)
}
val all = Suit.all
val allOrdered = Suit.allOrdered
val byId = Suit.byId
val byName = Suit.byName
val spade = Suit.Spade
}