scala - Implementing trait PartialOrdered[T] -
as exercise, supposed implement trait partialordered[t].
trait partialordered[t] { def below(that: t): boolean def < (that: t): boolean = (this below that) && !(that below this) /* followed other relations <=, >, >=, ==, .. */ }
a class k extends trait should have below implemented such that
a.below(b: k) = { true if <= b, false in other case
however, compiling gives following error:
value below not member of type parameter t def < (that: t): boolean = (this below that) && !(that below this) ^
so missing? in advance
edit: example class rectangle (in coordinate system), 2 opposing corners given, rectangle below if included
case class rectangle (x1: int, y1: int, x2: int, y2: int) extends partialordered[rectangle] { def below(r: rectangle): boolean = { val (rx, ry) = r.topleft val (tx, ty) = this.topleft tx >= rx && ty <= ry && tx + this.width <= rx + r.width && ty - this.height >= ry - r.height } def width: int = {...} def height: int = {...} def topleft:(int, int) = {...} }
you have tell scala t
subtype of partialordered[t]
:
trait partialordered[t <: partialordered[t]] { this: t => def below(that: t): boolean def < (that: t): boolean = (this below that) && !(that below this) /* followed other relations <=, >, >=, ==, .. */ }
see also: scala self-type: value not member error
Comments
Post a Comment