bod
03/17/2020, 4:01 PMval isStateXyz = xx.state in setOf(FOO, BAR, FOOBAR)
Is this idiomatic? Also, I think it may be a bit bad performance...
So I was thinking of creating this:
inline fun Any?.equalsAny(vararg objects: Any?): Boolean = objects.any { this == it }
and use it like this val isStateXyz = xx.equalsAny(FOO, BAR, FOOBAR)
Is it a good idea or should I just keep using in? Thoughts?araqnid
03/17/2020, 4:05 PMin setOf(..) is recreating the set each time. But in general I use in with EnumSet a lot, and then it’s just a bitmask checkMikael Alfredsson
03/17/2020, 4:07 PMsetof case, i assume that you only have a finite number of setof to test against, so initialize those inside the enum and create helpermethods that returns booleans.bod
03/17/2020, 4:08 PMin EnumSet.of( ... ?araqnid
03/17/2020, 4:22 PMin someSet where someSet is an EnumSetbod
03/17/2020, 4:26 PMbod
03/17/2020, 4:26 PMin thenbod
03/17/2020, 4:27 PMaraqnid
03/17/2020, 4:40 PMval SomeEnum.isStateXyz get() = this in xyzStatesaraqnid
03/17/2020, 4:42 PMoperator fun <T : Enum<T>> EnumSet<T>.getValue(owner: T, property: KProperty<*>): Boolean {
return owner in this
}
so then you can define:
val SomeEnum.isStateXyz by EnumSet.of(FOO, BAR, FOOBAR)araqnid
03/17/2020, 4:43 PMbod
03/17/2020, 4:44 PM