Hi, would like to know the rationale behind having...
# arrow
g
Hi, would like to know the rationale behind having some functions like
filterOrElse
as Extensions to
Either
instead of member functions? This won’t let to be used as a dot function when consumed from Java. I know Arrow is not designed for Java consumption, but is there any advantage of having this as extension over member? If not can these functions be made member fns, so we get an added advantage of consuming them from Java
r
Hi @Gopal S Akshintala, The reason is that Either is covariant in the left and right type arguments in order to help inference and not force people to ascribe the types manually. For this reason functions that take this argument like filterOrElse, flatMap etc in types that have more than one type argument can’t be declared as members inside Either without explicit unsafe Variance annotations that could potentially result in runtime error. If we were trying to add
filterOrElse
,
flatMap
or others as members we find the following issue:. The error type in Either is covariant but filterOrElse, flatMap expect it in contravariant or invariant positions.
message has been deleted
This is why in Arrow data types with two type arguments like Either need to express this functions as extension functions.
g
Oh I see… thanks alot @raulraja for detailed explanation. This makes sense 🙂
r
no problem! If we made the error invariant instead of
out
then we could support this as members but then you would not be able to accept subtypes, so this is the best we can do for now :)
g
ya understood… variance is the reason I wish to try Arrow’s either over Vavr’s (java Lib) Either… but I see the problem
Vavr is a Java lib where L, R are invariants
r
as for using those functions in Java, if there are any annotations we can use or ways to project them visible in Java with a nicer syntax that’s something we are open to.
g
ya I googled around for the same, but there are no solutions as of today. But if solved, can help in increasing Arrow’s adoption in java space as well… there is clearly a void in Java space for a functional lib