How might a Java program wrap a value into a scala.Either? For example, how would the following Scala code be written in Java?
Right("asdf")
Left(new Exception())
The following fails with “cannot find symbol method apply(java.lang.String)”
Right.apply("asdf");
The following fails with “cannot find symbol method apply(java.lang.Exception)”
Left.apply(new Exception());
If I understand the question correctly, assume you have the following Scala method:
you can call it from Java code by single creating
Eithersubclasses instances:If you want to pass functions from Java code, you have to implement
Function*trait depending on the function arity:In Java:
Of course in Scala it is much simpler as it supports lambdas on the syntax level: