natpryce
09/05/2017, 12:55 PMelect
09/05/2017, 1:08 PMInt
representing a group of enums, are used as mask for bitwise operation, afaik EnumSet
can't do thatdiesieben07
09/05/2017, 1:44 PMremoveAll
and retainAll
on EnumSet
will use bit-masking operators under the hood when called with an EnumSet
argument.elect
09/05/2017, 1:57 PM+=
operator but unfortunately it complains for ambiguity between the plusAssing
of Collections and Setsdiesieben07
09/05/2017, 2:02 PMval set = EnumSet.noneOf(RetentionPolicy::class.java)
set += RetentionPolicy.CLASS
elect
09/05/2017, 2:04 PMvar windowFlags = EnumSet.noneOf(WindowFlags::class.java)
if (noTitlebar[0]) windowFlags += WindowFlags.NoTitleBar
enum class WindowFlags(val i: Int) {
diesieben07
09/05/2017, 2:10 PMvar
? Then you get the ambiguity, because it can either add to the existing set or create a new set and assign it to the var
.elect
09/05/2017, 2:10 PMdiesieben07
09/05/2017, 2:10 PMval
only the former is possible. Really, you should always start with val
and only move to var
if you really need it.elect
09/05/2017, 2:11 PMvar
because it was an Int
beforediesieben07
09/05/2017, 2:11 PMelect
09/05/2017, 2:11 PMEnumSet.noneOf
diesieben07
09/05/2017, 2:13 PMEnumSet
. But due to the internal structure of an EnumSet
you then need to specify the type using a Class
instance. You could write a function in Kotlin using reified type parameters to avoid having to pass that in explicitly.elect
09/05/2017, 2:15 PMclass type
, not an instance, or?inline fun <reified E : Enum<E>> enumSetOf(): EnumSet<E> = EnumSet.noneOf(E::class.java)
diesieben07
09/05/2017, 2:16 PMMyEnum::class.java
is an instance of java.lang.Class
(which is a class itself).elect
09/05/2017, 2:17 PMdiesieben07
09/05/2017, 2:17 PMEnumSet
.elect
09/05/2017, 2:19 PMcontains
?infixed has
diesieben07
09/05/2017, 2:19 PMin
operator, which is basically just an alias for `contains`:
MyEnum.A in myEnumSet
elect
09/05/2017, 2:21 PMif(flags has x)
diesieben07
09/05/2017, 2:21 PMelect
09/05/2017, 2:22 PMflags
diesieben07
09/05/2017, 2:23 PMhas
easily, which delegates to contains
, if you want to keep the existing order.elect
09/05/2017, 2:29 PM|
-> add
, & ~
-> remove
xor
?diesieben07
09/05/2017, 2:32 PMif (thing in set) set -= thing else set += thing
So, probably worth another extension.natpryce
09/05/2017, 3:34 PMEnumSet<T:Enum<T>>
in the Kotlin library. I find Java collection’s default mutability very off-putting now.karelpeeters
09/05/2017, 3:57 PMSet
reference, then you can't modify it. Maybe EnumSet
has some more methods though, not sure.elect
09/05/2017, 4:17 PMEnumSet
now I can overcome the kotlin limitation of flags |= property
flags += property