nfrankel
12/16/2020, 11:18 AMwhen
but then, how do i compute the id
so i can use it in the later evaluations?Vampire
12/16/2020, 11:38 AMval id = string?.let { Json.parse(it) }?.asObject()?.get("id")
return when {
id == null -> null
id.isString -> Util.entry(id.asString(), string)
else -> Util.entry(id.asObject(), string)
}
kenkyee
12/16/2020, 11:45 AMVampire
12/16/2020, 12:24 PMstreetsofboston
12/16/2020, 1:46 PMwhen(val id =...) {
...
}
Vampire
12/16/2020, 1:53 PMid
,
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.nfrankel
12/16/2020, 2:18 PMVampire
12/16/2020, 2:19 PMVampire
12/16/2020, 2:21 PMwhen(val ...) { ... }
then the subject is captured as that val
so that you can use it within the branches easily,
but the when expression variant would still be the one with parameter and thus you cannot use arbitrary expressions but only supplying values or ranges that are compared or checking types and as it is not is String
but .isString
you need the parameterless variant