nfrankel
12/16/2020, 11:17 AMwhen
block?
return if (string == null) null
else {
val id = Json.parse(string)?.asObject()?.get("id")
when {
id == null -> null
id.isString -> Util.entry(id.asString(), string)
else -> Util.entry(id.asObject(), string)
}
}
here, i first check a if
then compute the id
then execute my when
using the computed id
it feels not as readable as it couldAnimesh Sahu
12/16/2020, 11:50 AMwhen(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)
}
Animesh Sahu
12/16/2020, 11:55 AMnfrankel
12/16/2020, 2:18 PMMilan Hruban
12/16/2020, 2:55 PMval id = string?.let(Json::parse)?.asObject()?.get("id")
return when (id?.isString) {
null -> null
true -> Util.entry(id.asString(), string)
false -> Util.entry(id.asObject(), string)
}
also, if isString
didn't return Boolean? but rather Boolean, I would do
val id = string?.let(Json::parse)?.asObject()?.get("id") ?: return null
return if(id.isString) Util.entry(id.asString(), string) else Util.entry(id.asObject(), string)
nfrankel
12/16/2020, 2:57 PMMilan Hruban
12/16/2020, 2:58 PMnfrankel
12/16/2020, 3:26 PM