Hi people, Does kotlin support exhaustive when wit...
# android
s
Hi people, Does kotlin support exhaustive when without assigning a variable or without libraries?
e
v
If you decide to go the assignment way, you could at least make it look fancy by making it a face watching the exhaustiveness like `val
_
=` or
val o_O =
. :-) Another way that is not an assignment, does not need a library, does not pollute the
Any
namespace and does not impact runtime performance as it is an inlined no-op is this variant:
Copy code
when (...) {
    ...
}.let { /* it be exhaustive */ }
j
There were another approach via object + infix function so you get:
Copy code
Do exhaustive when (...) { }
v
Hm, also nice, though with runtime effect as it cannot be inlined to a no-op, right?
j
I think so
e
Proguard/R8 could be configured to eliminate it
but even so, I would expect a hack like
Copy code
val Do: Nothing?
    inline get() = null
infix inline fun Any?.exhaustive(`when`: Any?) = Unit
to have no runtime impact
­čĹŹ 1
s
That's nice options I need to try.
How would you use that @ephemient?
personally I would just throw a
.let {}
on the end, though
v
the infix fun on Any?
Shouldn't it be more like
Copy code
object Do {
    inline infix fun exhaustive(`when`: Any?) = Unit
}
Or do you expect runtime impact on that version?
j
Yeah, with the object you are not forced to use Any to avoid suggesting it for everything
s
Kotlin needs to release an official Exhaustive when...
j
I think it will be in 1.6
e
@Vampire with the
object
version, you still have the runtime impact of
Do.getInstance()
s
Cool, we are near.