Gopal S Akshintala
03/05/2021, 10:26 AMtypealias _Validator_<_ValidatableT_, _FailureT_> = _suspend_ (_ValidatableT_) -> _Either_<_FailureT_, Any?>
Within Kotlin, I use it like this as a data type to assign to a lamda val validateParent3X: _Validator_<_Egg_, _ValidationFailure_> = {...}
But I cannot use typealias from Java. What is the idiomatic way to use this Function type as data type on Java?
I see two unknows I have for java interoperability. 1. How can I idimotically refer a Function type 2. Function type being suspend
simon.vergauwen
03/05/2021, 10:39 AM_Validator_<_ValidatableT_, _FailureT_>
should be (_ValidatableT, Continution<Any?>_) -> Any?
from Java, no?Gopal S Akshintala
03/05/2021, 10:42 AMsuspend
Gopal S Akshintala
03/05/2021, 10:43 AMsimon.vergauwen
03/05/2021, 10:53 AMsuspend
from Java it always becomes callback based using Continution
simon.vergauwen
03/05/2021, 10:53 AMsuspend fun example(i: Int): Int = i + 1
is actually:simon.vergauwen
03/05/2021, 10:53 AMpublic Object example(Int i, $cont Continuation<Object>)
afaikpakoito
03/05/2021, 11:03 AMBut I cannot use typealias from Java. What is the idiomatic way to use this Function type as data type on Java?At worst you’d have to write an interface that extends Kotlin’s function interfaces with the parameters Simon has indicated
pakoito
03/05/2021, 11:03 AM() -> A
in Java is kotlin.Function0 or something like thatGopal S Akshintala
03/05/2021, 11:04 AMsuspend
for now and Instead of typealias, I used interface
interface Validator<ValidatableT, FailureT> {
fun validate(validatable: ValidatableT): Either<FailureT, Any?>
}
Now I am able to use it in java this way
Validator<Egg, ValidationFailure> abc = egg -> new Either.Right(new Egg(1, new Yolk(Condition.GOOD, Color.GOLD)));
pakoito
03/05/2021, 11:04 AMpakoito
03/05/2021, 11:05 AMfun interface
in Kotlin, you should get SAM tooGopal S Akshintala
03/05/2021, 11:05 AMGopal S Akshintala
03/05/2021, 11:06 AMGopal S Akshintala
03/05/2021, 11:06 AMValidator {…}
Gopal S Akshintala
03/05/2021, 11:34 AMval validate1SimpleX: Validator<Egg, ValidationFailure> = Validator {
if (simpleRule(it)) true.right() else NO_EGG_TO_VALIDATE_1.left()
}
pakoito
03/05/2021, 12:34 PMI see there is no way to assign lambda to interface in Kotlinyou can with inheritance
pakoito
03/05/2021, 12:34 PMinterface Validator<ValidatableT, FailureT, ResultT>: (ValidatableT) -> Either<FailureT, ResultT>
pakoito
03/05/2021, 12:35 PMGopal S Akshintala
03/05/2021, 12:41 PMMarius Kotsbak
03/05/2021, 3:11 PMMarius Kotsbak
03/05/2021, 3:14 PMGopal S Akshintala
03/05/2021, 3:29 PMsimon.vergauwen
03/05/2021, 4:06 PMMarius Kotsbak
04/21/2021, 11:24 AM