Kotlin introduced the new type: Result. I use it as a completion handler of several functions like this:
fun myFunction(completion: (Result<Boolean>) -> Unit)
Unfortunately I cannot use it in java. Java doesn't propose me getters like getOrNull or even isSuccess/isFailure.
How could I use it? Thanks
The Result class is designed to capture generic failures of Kotlin functions for their latter processing and should be used in general-purpose API like futures, etc, that deal with invocation of Kotlin code blocks and must be able to represent both a successful and a failed result of execution.
@Throws annotation indicates what exceptions should be declared by a function when compiled to a JVM method.
The Either type is used to represent a value that can have two possible types. It is common to see Either used to represent a success value or a failure value, although that doesn't have to be the case. Although Kotlin doesn't come with an Either as part of the standard library, it's very easy to add one.
Unit: Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.
There is no standard support for Result
like types in Java but you can always use third-party libraries like HubSpot/algebra
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With