Union types

Consider writing a jsonObjectOf function (like the bundleOf function from Anko). Today, you might write something like:

fun jsonObjectOf(vararg pairs: Pair<String, Any?>) = JSONObject().apply {
  for ((key, value) in pairs) {
    when (value) {
      null -> putNull(key)
      is String -> put(key, value)
      is Boolean -> put(key, value)
      is Double -> put(key, value)
      is Int -> put(key, value)
      is Long -> put(key, value)
      else -> throw UnsupportedOperationException()

There is no way to enforce compile-time type constraints for the arguments. Wouldn’t it be better to enforce that the arguments are of some union type?