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
streetsofboston
12/18/2022, 12:37 AM
I think calling "if (a.isPublic) shift(DomainError())" may work, or "if (a.isPublic) DomainError().left().bind()"
streetsofboston
12/18/2022, 12:41 AM
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
S.
12/18/2022, 1:00 AM
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.