Created
December 18, 2017 17:09
-
-
Save ZakTaccardi/c373c1fd04521ca0ea2a159cd05e649e to your computer and use it in GitHub Desktop.
Idea for better sealed class JVM interop for kotlin compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment