Julien Cayzac
04/24/2019, 1:34 AMwhen
. The require
clause implies element
cannot be a JsonNull
:
fun unbox(element: JsonElement): Any {
require(element !is JsonNull) { "The token contains null values" }
return when (element) { // error: JsonNull case missing
is JsonLiteral -> element.body
is JsonArray -> element.filter { it != JsonNull }.map { unbox(it) }
is JsonObject -> element.content.filterValues { it != JsonNull }.mapValues { unbox(it.value) }
}
}
Mike
04/24/2019, 1:46 AMJulien Cayzac
04/24/2019, 1:50 AMJsonNull -> require(false) { "The token contains null values" }
but I was curiousJulien Cayzac
04/24/2019, 1:50 AMimplies
really works.Mike
04/24/2019, 1:57 AMrequire(false) {"message"}
. Just use throw IllegalArgumentException("The token contains null values")
. It’s the same thing with less indirection.Mike
04/24/2019, 1:58 AMJulien Cayzac
04/24/2019, 8:29 AMIt’s the same thing with less indirection.It's longer to type.
Mike
04/24/2019, 10:14 AMJulien Cayzac
04/25/2019, 12:29 AMrequire
than if (condition) throw LegacyJDKExceptionClassFrom20YearsAgo()
as a failed requirement, especially since the actual exception class doesn't matter since it's not part of our public API and should be let to crash.Mike
04/25/2019, 1:19 AMrequire(element !is JsonNull)....
line and instead being forced to explicitly state it in the when
statement.
So you put your when statement as JsonNull -> require(false) { "The token contains null values"}
and I’m suggesting that is harder, or at least unexpected, to read (require(false)
just doesn’t feel right to me).
So I’m suggesting it should be explicit, and just throw the exception.
JsonNull -> throw IllegalArgumentException("The token contains null values")
.Mike
04/25/2019, 1:40 AM