scala - Override trait's generic method without casting -
i planning have trait "value" attribute can extended multiple classes. need able compare different instances of trait between each other. firstly want check value defined on trait - if thats same across 2 instances, want trigger child-specific method compare 2 child instances of same type each other.
i hope explained enough...
i have below code written, childone class doesnt compile - error getting:
class childone needs abstract, since method childcheck in trait parenttype of type (other: childone.this.t)boolean not defined (note parenttype.this.t not match com.cards.childone)
trait parenttype { val value: int type t <: parenttype def parentcheck(other: t): boolean = { if (this.value == other.value) childcheck(other) else this.value > other.value } def childcheck(other: t): boolean } case class childone(name: string) extends parenttype { val value = 1 override def childcheck(other: childone): boolean = { true //some custom logic each child... } }
i change parameter of child's method parenttype , cast it, wanted avoid , wondering there better way of doing want achieve?
any appreciated.
you declare abstract type t in parenttype
, without overriding in childone
. can fix overriding t in chileone
:
case class childone(name: string) extends parenttype { val value = 1 override type t = childone // <---- override def childcheck(other: childone): boolean = { true //some custom logic each child... } }
Comments
Post a Comment