https://kotlinlang.org logo
#arrow
Title
# arrow
n

Nick Kleban

03/06/2024, 8:51 AM
What is arrow-idiomatic way to return typed error from function that returns Unit?
y

Youssef Shoaib [MOD]

03/06/2024, 8:56 AM
Raise<ErrorType>.myFun(): Unit
n

Nick Kleban

03/06/2024, 8:59 AM
Thank you. One more question - If I use
either{ }
in a function that returns Either<Error, Unit>, do I need to bind Unit or something?
s

simon.vergauwen

03/06/2024, 8:59 AM
It's the safest option to use
Raise
directly, you can also use
Either<ErrorType, Unit>
but then you need to make sure to always call
bind
. There is a detekt plugin to check that.
Yes, you need to make sure to call
bind
on
Either<Error, Unit>
, otherwise you'd swallow/ignore to error. Or, recover using
getOrElse
, etc.
That's why
Raise<ErrorType>.myFun(): Unit
is safest, you can call it inside
either<ErrorType, Unit> { myFun() }
but you don't need to call
bind
.
Raise<ErrorType>
gives you the exact same API as you get inside
either<ErrorType, Unit> { }
, but
Either
is perfectly valid if that's what you're using everywhere in your project ☺️ If it's a
private
function you could still consider
Raise
IMO.
🙌 2
n

Nick Kleban

03/06/2024, 9:03 AM
Thank you very much, got it
🙌 1
2 Views