Scala -- mutually exclusive traits -
is there way define collection of alternatives of common type:
trait mutability trait mutable extends mutability trait immutable extends mutability
and have compiler preclude like:
object hat extends mutable immutable
i believe can force some compiler error having common, conflicting member error message bit oblique:
trait mutability trait mutable extends mutability { protected val conflict = true } trait immutable extends mutability { protected val conflict = true } object hat extends mutable immutable <console>:10: error: object hat inherits conflicting members: value conflict in class immutable$class of type boolean , value conflict in class mutable$class of type boolean (note: can resolved declaring override in object hat.) object hat extends immutable mutable
is there more direct way express constraint , not permit work around taking hint offered compiler (override 'conflict' in hat)?
thanks insights
i think might work
sealed trait mutability case object immutable extends mutability case object mutable extends mutability trait mutabilitylevel[a <: mutability] class foo extends mutabilitylevel[immutable.type]
this (ab?)uses fact can't extend same trait twice different parameterization
scala> class foo extends mutabilitylevel[immutable.type] mutabilitylevel[mutable.type] <console>:11: error: illegal inheritance; self-type foo not conform mutabilitylevel[immutable.type]'s selftype mutabilitylevel[immutable.type] class foo extends mutabilitylevel[immutable.type] mutabilitylevel[mutable.type] ^ <console>:11: error: illegal inheritance; self-type foo not conform mutabilitylevel[mutable.type]'s selftype mutabilitylevel[mutable.type] class foo extends mutabilitylevel[immutable.type] mutabilitylevel[mutable.type]
however..
scala> class foo extends mutabilitylevel[mutability] defined class foo
Comments
Post a Comment