If i want to perform an effect full operation like...
# arrow
j
If i want to perform an effect full operation like storing in a database, how could be the signature of the method I have an idea that is :
suspend fun killSession(): Either<LowUnprotectedError, Unit>
use void to represent the the effect full operation but feels wear, i think should be a better way to represent this kind of operations
👌 2
a
I think that’s perfect 👌
since you don’t need to return anything, the Unit there matches that intention, and having it inside an Either means you can flatmap with other ops and just ignore this unit result
☝️ 3
r
Alternatively you may create your own type in place of Unit to be more explicit
object SessionKilled
even if you are ignoring it, it’s more clear what it means and not open to interpretation as to what happen when someone reads the function signature:
Copy code
suspend fun killSession(): Either<LowUnprotectedError, SessionKilled>
I prefer this style if the api is public since Unit has the double meaning of being auto coerced in blocks if you don’t return anything. This would make sure you never forget a return in the right place.
🔝 2
j
Thanks so much, i did not think i was going to receive an answer. thanks so much for taking the time to help me with my understanding. I left my function as
suspend fun killSession(): Either<LowUnprotectedError, Unit>
but @raulraja`s option is a very good point, for sure i’ll do the respective refactor. Thanks
👍 1