Alejo
06/02/2022, 10:22 AMBoolean variable from another one of type Boolean? and if the value is null then I will want to check if a value is present in another list and use that value for the initialization. Here is the code:
val statuses = listOf("pending", "on-hold", "auto-draft")
val status = "pending"
val isEditable: Boolean? = true
val result = isEditable ?: status in statuses
print(result)
But this initialization using the Elvis operator always returns false , if I move the status in statuses to a different variable or I wrap it in a run {status in statuses} then the code works as expected.
Am I missing something here or this is a bug?
You can test the example here: https://pl.kotl.in/4QDgoNvYimax.denissov
06/02/2022, 10:29 AMval result = isEditable ?: (status in statuses)Joffrey
06/02/2022, 10:29 AMisEditable ?: status in statuses is understood as (isEditable ?: status) in statuses, so you need clarifying parentheses around your in check:
val result = isEditable ?: (status in statuses)
However, I'm surprised this compiles without the parentheses. There might be a bug in how elvis type is considered for contains:
statuses.contains(42 ?: "bob") // compiles fine, returns false
statuses.contains(42) // compile errorJoffrey
06/02/2022, 10:31 AMList is covariant in T and contains can be understood as being applied to a List<Comparable> here. The type of 42 ?: "bob" is likely Comparable and thus the contains call compiles.
The same could be said for 42 alone, but I guess the compiler doesn't go that far. It does work with explicit type argument:
statuses.contains<Comparable<*>>(42) // compiles fineAlejo
06/02/2022, 11:52 AMAlejo
06/02/2022, 11:54 AM