Rob Elliot
04/21/2021, 1:42 PMwhen (enumValue) {
Enum1, Enum2 -> doSomethingImperative()
else -> Unit
}
as an alternative to
val certainEnumConstants = setOf(Enum1, Enum2) // defined in an object, so allocated once for the life of the runtime - recent edit in light of confusion in thread
if (enumValue in certainEnumConstants) {
doSomethingImperative()
}
I like the lack of need to define and name a val enums: Set<EnumType> in the first, but I don’t like the need for an else branch.Vampire
04/21/2021, 1:48 PMcertainEnumConstants?
This should be the same, shouldn't it?
if (enumValue in setOf(Enum1, Enum2)) {
doSomethingImperative()
}
And why do you need an else, if the when is not used as an expression, it does not have to be exhaustive.natario1
04/21/2021, 1:48 PMRob Elliot
04/21/2021, 2:06 PMcertainEnumConstants, but that in a when check it doesn’t. Perhaps the compiler (or JIT?) is cleverer than that - I should check.
2) You’re right - it’s a warning rather than an error. I’ve got in the habit of treating warnings as errors. I can’t suppress that warning, irritatingly…mkrussel
04/21/2021, 2:13 PMRob Elliot
04/21/2021, 2:13 PMif (enumValue == Enum1 || enumValue == Enum2 || ...)Rob Elliot
04/21/2021, 2:22 PMif (SetsKt.setOf(new MyEnum[]{Enum1, Enum2}).contains(enumValue)) {
this.doSomethingImperative();
}
so it’s potentially allocating an array and a set on each invocation.ephemient
04/21/2021, 2:34 PMephemient
04/21/2021, 2:40 PMwhen is fine, and as Vampire says, doesn't require else -> if you're not using the result as an expression. and if you decide to create a set, Java's EnumSet might be interesting (it is a bit mask of element ordinals underneath)Vampire
04/21/2021, 2:41 PMcertainEnumConstants somewhere else and then use it repeatedly.
Then it makes a difference of course.Nir
04/21/2021, 2:46 PMwhen always tends to make me shy away from it for simple use casesRob Elliot
04/21/2021, 2:47 PMcertainEnumConstants was a constant on an object.Nir
04/21/2021, 2:47 PMif (enumValue in listOf(Enum1, Enum2))Nir
04/21/2021, 2:48 PMsequenceOfNir
04/21/2021, 2:48 PMephemient
04/21/2021, 2:49 PMephemient
04/21/2021, 2:49 PMNir
04/21/2021, 2:50 PMNir
04/21/2021, 2:50 PMlistOf or setOf approachNir
04/21/2021, 2:50 PMNir
04/21/2021, 2:50 PMephemient
04/21/2021, 2:52 PMNir
04/21/2021, 2:52 PMif (enumValue in sequence { yield(Enum1); yield(Enum2) }) 😛ephemient
04/21/2021, 2:52 PMNir
04/21/2021, 2:54 PMephemient
04/21/2021, 3:59 PMsequenceOf()::contains) then it's not a valid compile-time optimization. and in this case, it's too complex for the JIT to look into either.Nir
04/21/2021, 4:00 PMephemient
04/21/2021, 4:04 PMa in b..c == a >= b && a <= c, and that creating the range object has no side effects and can be omitted)ephemient
04/21/2021, 4:06 PMlistOf()::contains and sequenceOf()::contains to a chain of `==`/`&&`. but it's not currently the caseephemient
04/21/2021, 4:08 PMenumValue in arrayOf(Enum1, Enum2) has less - it's a vararg array creation just like all the `listOf()`/`sequenceOf()`, but nothing more than thatephemient
04/21/2021, 4:28 PMEnumSet.of() has a number of overloads so you might not even be using varargs thereTravis Romney
04/21/2021, 7:13 PMephemient
04/21/2021, 7:24 PMEnumType.values() or enumValues<EnumType>() (kotlin-style)... but it sounded like Rob wanted to act on just a subset of the enum values. (also it creates a new Array each time, for compatibility reasons)