<@U0BFDUP0E> would those be a Java 10+ only? or so...
# language-proposals
z
@jw would those be a Java 10+ only? or something we’ll be able to use in the foreseeable future on android. I think the compiler, given an annotation (similar to
@JvmStatic
) could improve the situation now by generating a special method on the base sealed class that takes a callback as a parameter, where the callback is each type. Example:
Copy code
// generates callback and method to handle that callback for better Java interop
annotation class JvmCallback

@JvmCallback
sealed class Result {

  data class Success(val successId: String) : Result()
  data class Error(val exception: Exception) : Result()

  // would be automatically generated because of JvmCallback
  fun handle(callback: Result.Callback) {
    return when (this) {
      is Result.Success -> callback.onSuccess(this)
      is Result.Error -> callback.onError(this)
    }
  }

  // would be automatically generated because of JvmCallback
  interface Callback {

    fun onSuccess(value: Result.Success)
    fun onError(value: Result.Error)
  }
}

fun fromJavaExample(result: Result) {
  // or maybe a SAM function for each implementing type for better syntax from Java?
  result.handle(object : Result.Callback {
    override fun onSuccess(value: Result.Success) {
      TODO("not implemented")
    }

    override fun onError(value: Result.Error) {
      TODO("not implemented")
    }
  })
}