I had a weird bug today regarding `when`. I have a...
# announcements
m
I had a weird bug today regarding
when
. I have a field that holds an enum value and in another place, I have an (exhaustive) when given that field. So the parameter of the
when
was of type
T
. Then, I changed the field to be instead a
LiveData<T>
. A lot of places in my source threw errors and I fixed them by calling
.value
on the field. But the
when
did not threw an error (as the when does not return anything, the fact that is is no longer exhausive is not an error). But why can I even write:
Copy code
when(status /* type = LiveData<T>*/) {
   T.FIRST_VALUE -> { ... }
   T.SECOND_VALUE -> { ... } 
   T.THIRD_VALUE -> { ... }
as status can never be any of these types? When I try the same structure with a String as parameter, it throws an error (incompatible types).
I cannot find a logic that checks out, if the parameter is LiveData of anything, it does not report an error, if its a Pair for example, it does. I cannot check if the parameter is String, but I can check for an instance of any random interface in my project
d
I don't know the answer, but I can provide some logic. LiveData is abstract, its equals function probably is open, so the compiler cant make assumptions at compile time.
Unlike Pair, String
m
I tried it with a class that is not open (my own subclass of LiveData) and it still worked like LiveData
d
Then the compiler is probably dealing with special cases