james
11/14/2019, 11:45 PMit.nullableField
is null. I've tried the following, but it doesn't work as expected because the smart cast fails, as seen in comment:
repo.getThings()
.mapNotNull {
if (it.nullableField != null) {
SomeOtherThing(
id = it.id,
name = it.name,
requiredField = it.nullableField // compiler error: Smart cast to String is impossible because it.nullableField is a public API property declared in a different module
)
} else null
}
streetsofboston
11/14/2019, 11:48 PM.filter { it.nullableField != null }
into the chain.
Then just do .map { SomeOtherThing( ..., ..., requiredField = it.nullableField!!) }
!!
check; smart-casting won’t work in this casejames
11/14/2019, 11:49 PMit.nullableField
where my comment is.
both of these seem to work, but I'm wondering what the idiomatic way is?filter
feels more readable at leastbasher
11/14/2019, 11:51 PMrepo.getThings()
.mapNotNull { thing ->
thing.nullableField?.let {
SomeOtherThing(
id = thing.id,
name = thing.name,
requiredField = it
)
}
}
james
11/14/2019, 11:58 PMfilter
basher
11/14/2019, 11:58 PMjames
11/14/2019, 11:58 PMMatteo Mirk
11/19/2019, 12:29 PMfilter
is the best option