Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin result type in java

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

like image 786
DEADBEEF Avatar asked Mar 10 '20 22:03

DEADBEEF


People also ask

What is Kotlin result?

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.

What is throwable Kotlin?

@Throws annotation indicates what exceptions should be declared by a function when compiled to a JVM method.

What is either in Kotlin?

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.

What is a unit Kotlin?

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.


1 Answers

There is no standard support for Result like types in Java but you can always use third-party libraries like HubSpot/algebra.

like image 102
Andrea Bergonzo Avatar answered Oct 23 '22 09:10

Andrea Bergonzo