I don't think you would win much.
This could be used to restrict the scope of
id
,
but the
when
result is directly returned, so there is nothing the
id
could leak into.
Besides that whyever, then
id
cannot be smart-casted to not-null in the second branch, but you need
return when(val id = string?.let { Json.parse(it) }?.asObject()?.get("id")) {
id == null -> null
id!!.isString -> Util.entry(id.asString(), string)
else -> Util.entry(id.asObject(), string)
}
or
return when(val id = string?.let { Json.parse(it) }?.asObject()?.get("id")) {
id == null -> null
id?.isString -> Util.entry(id?.asString(), string)
else -> Util.entry(id?.asObject(), string)
}
or similar.
But I think this is more a compiler bug.