i want create multiple enumeration objects, inheriting 1 parent. reason why want them inheriting 1 parent want create collection of enumerations objects , group them under type.
object searchable{ trait searchflags{ self: enumeration => type value = self.value } } object searchablebylocation{ object searchflags extends enumeration{ val flag1 = value } } object searchablebyage{ object searchflags extends enumeration{ val flag1 = value } } object test extends app{ //i cannot refer members of trait directly, //underlying type of set invalid val flags: set[ searchable.searchflags.value ] = set( searchablebylocation.searchflags.flag1, searchablebyage.searchflags.flag1 ) } how go achieving this?
well, scala's enumeration objects nothing more series of values of shared type. need create common trait , guarantee constants of type. works:
trait commonbase object enum1 extends enumeration { protected class enum1val extends val commonbase protected final def enum1value() = new enum1val() val a, b, c = enum1value() } object enum2 extends enumeration { protected class enum2val extends val commonbase protected final def enum2value() = new enum2val() val d, e, f = enum2value() } def main(args: array[string]): unit = { val flags: set[ commonbase ] = set( enum1.a, enum2.d ) } of course, need create own value type , own factory method produce new type instead of default type comes enumeration.
Comments
Post a Comment