Is there a better way to write this: ```inline fun...
# codereview
d
Is there a better way to write this:
Copy code
inline fun <reified V> jsonConverter(
) = when {
    JsonObjectBuilder::class.java.isAssignableFrom(V::class.java) -> JsonObjectBuilder.jsonConverter
    JsonNode::class.java.isAssignableFrom(V::class.java) -> {
        { it: JsonNode<*> -> it }
    }

    else -> when (V::class) {
        Int::class -> Int.jsonConverter
        Double::class -> Double.jsonConverter
        Float::class -> Float.jsonConverter
        Long::class -> Long.jsonConverter
        String::class -> String.jsonConverter
        else -> throw IllegalArgumentException("Unsupported type: ${V::class}")
    }
} as (V) -> JsonNode<*>
e
IMO switching on
reified
type parameters is a bad pattern. it needs to be resolved statically anyway, so you might as well have separate functions (possibly overloaded) and turn the
IllegalArgumentException
case into a compile-time error (no such function) instead
d
I agree, but this is the bottom most of a call hierarchy that would need to be duplicated for each type. The only other way to do this suitably would be with code generation, which really is not what I want to do.