https://kotlinlang.org logo
#codereview
Title
# codereview
j

jwass

10/27/2021, 1:56 PM
Hello! I have a number of classes that might or might not be resolved to some external data source. So that just means wrapping in a container. A combination of optional / decorator pattern.
Copy code
sealed class Resolvable<T>
class Resolved<T>(val value: T, val pk: Long) : Resolvable<T>()
class Unresolved<T>(val value: T) : Resolvable<T>() {
    fun resolved(pk: Long) = Resolved(value, pk)
}
Now I want to take a
Collection<Resolvable<ItemIdentifier>>
and filter only unresolved ones, into a
Collection<Unresolved<ItemIdentifier>>
. This works:
Copy code
val x : Collection<Unresolved<ItemIdentifier>> = itemIdentifiers.mapNotNull { when(it) {is Unresolved -> it else -> null }}
But I wonder if there's a more idiomatic way to do it? (type annotation on
x
just for clarity).
o

Orhan Tozan

10/27/2021, 2:09 PM
Copy code
val x: Collection<Unresolved<ItemIdentifier>> = itemIdentifiers.filterIsInstance<Unresolved<ItemIdentifier>>()
j

jwass

10/27/2021, 3:04 PM
!!!! Thank you! Don't know why I didn't see that.
o

Orhan Tozan

10/28/2021, 10:20 AM
Because with an if else, the compiler thinks there is a chance that there would be an else. When basically assures the compiler those are the only 2 cases
j

jwass

10/28/2021, 10:20 AM
So the same sealed class guarantees just don't apply to
if
?
d

dave08

10/28/2021, 11:57 AM
I think if you put
else error("Invalid case")
after that
if
, it should work.
But it's probably not as nice...
j

jwass

10/28/2021, 2:05 PM
Thank you. Not as nice and also not as correct I think. I think there's a clear nudge to use
when
.
t

Tobias Suchalla

10/29/2021, 6:10 AM
BTW, there might be an even easier option (as seen here).
Copy code
sealed class Resolvable<T> {
    abstract val value: T
}

data class Resolved<T>(
    override val value: T, 
    val pk: Long
) : Resolvable<T>()

data class Unresolved<T>(
    override val value: T
) : Resolvable<T>()
That way there is always a
value
on a
Resolvable<T>
, no need to unwrap.
j

jwass

11/23/2021, 9:00 AM
Thank you! Sorry I didn’t get the notification about this. I did try this approach actually. It works… but in the end it was too verbose to use cleanly.
3 Views