My goal is to use Arrow in a java project at our c...
# arrow
g
My goal is to use Arrow in a java project at our company ‘Salesforce’ How can I use Kotlin Function type as a Data type within Java? For example: I have a typealias 
typealias _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
s
_Validator_<_ValidatableT_, _FailureT_>
should be
(_ValidatableT, Continution<Any?>_) -> Any?
from Java, no?
g
didn’t complete get it, but I see two unknows I have for java interoperability. 1. How can I idimotically refer a Function type 2. Function type being
suspend
My goal is to make use of arrow in a Java project at our company Salesforce, so I am researching on interoperability
s
Afaik if you interopt with
suspend
from Java it always becomes callback based using
Continution
Since
suspend fun example(i: Int): Int = i + 1
is actually:
public Object example(Int i, $cont Continuation<Object>)
afaik
p
But 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
() -> A
in Java is kotlin.Function0 or something like that
g
Got it! I removed
suspend
for now and Instead of typealias, I used interface
Copy code
interface Validator<ValidatableT, FailureT> {
    fun validate(validatable: ValidatableT): Either<FailureT, Any?>
}
Now I am able to use it in java this way
Copy code
Validator<Egg, ValidationFailure> abc = egg -> new Either.Right(new Egg(1, new Yolk(Condition.GOOD, Color.GOLD)));
p
great
do
fun interface
in Kotlin, you should get SAM too
g
I explored them, but they are only useful within Kotlin right?
I dnt see a use of them over interface
extra overhead is I had to use name before lambda, like
Validator {…}
Oh! I was wrong about that, my compiler fooled me.. I see there is no way to assign lambda to interface in Kotlin. It has to be a SAM and I have to use it this way
Copy code
val validate1SimpleX: Validator<Egg, ValidationFailure> = Validator {
    if (simpleRule(it)) true.right() else NO_EGG_TO_VALIDATE_1.left()
}
p
I see there is no way to assign lambda to interface in Kotlin
you can with inheritance
interface Validator<ValidatableT, FailureT, ResultT>: (ValidatableT) -> Either<FailureT, ResultT>
you can probably throw fun interface there too
g
With inheritance I see the same problem
m
I suspect using Arrow in Java is not so good idea. Have you considered https://www.vavr.io/ instead? It should supply much of the same native to Java.
g
Yes I currently use it, but there doesn't seem to be active development on that project
s
Afaik the developer that used to work on Vavr stopped working on it since he moved away from Java but I'm not 100% sure.
m
Ah, that is possible. Try to get Salesforce to move your module to Kotlin or Scala then 🙂