```fun a() : Either<DomainError, Int> = eith...
# arrow
s
Copy code
fun a() : Either<DomainError, Int> = either {
    val a = getById(1).bind()
    if (a.isPublic) return@either DomainError().left()
    a.id
}
Why does
return@either Error.left()
affect the
Either.Right
here? I'm getting
Type mismatch. Required: Int Found: Either<DomainError, Nothing
.
DomainError().left().bind()
would comply with the signature but warns that ``Returning type parameter has been inferred to Nothing implicitly` . What is the proper way to return early with a Either.Left in such a case?
s
I think calling "if (a.isPublic) shift(DomainError())" may work, or "if (a.isPublic) DomainError().left().bind()"
In the ”either" lambda you must return a value of the Right's type (Int). To cause this "either {...}" to return an Either.Left type (DomainError) you need to shortcut the lambda by binding to an Either.Left, or by shifting to an error (I'm a bit rusty on the shifting and such 😁, but I think explained the gist of it)
s
yeah thanks. Though I'm not sure which type B is supposed to be.
fun <B> shift(r: R): B
because it cannot be inferred. In Effect it states that both, A and R are mapped to B.
DomainError().left().bind()
infers
Nothing
but with a warning that it could produce runtime exceptions.
j
It looks like
B
is
Int
, in your case.
s
Okay, thanks for the clarification
o
Relevant: The return type of
shift
was changed to `Nothing`: https://github.com/arrow-kt/arrow/issues/2810 Arguably that should always have been the case - execution does not continue after
shift
. It's akin to other
Nothing
producers such as
return
or
throw
.