Can someone point me to a summary of how the inter...
# arrow
d
Can someone point me to a summary of how the internal implementation for computation short-circuiting works for
either
? Also, has this internal approach changed for Arrow 1.2 (as well as the obvious change for
Raise<E>
)?
r
🙌 3
arrow intensifies 1
Hope it helps.
y
Looking at the internal implementation definitely helps a lot too, but if you want the TL;DR: when you
raise()
, a
RaiseCancellationException
(usually the
NoTrace
version of it because it's more efficient) is thrown, and inside it contains your
Raise
instance, and the value you raised. Then, every
fold
block (which all raise builders ultimately call) will end up identity-checking that
RaiseCancellationException.raise === expectedRaiseInstance
. If that's true, then it casts the value to the expected type, and it's passed to the
recover: (Error) -> R
lambda that
fold
accepts. It all boils down to
myRaise.raise(errorVal) == throw RaiseCancellationException(myRaise, errorVal)
and
fold == try { block(myRaise) } catch(e: CancellationException) { recover(e.raisedOrRethrow(myRaise)) }
where
e.raisedOrRethrow(myRaise) == if(e is RCE && e.raise == myRaise) e.value else throw e
👍 1
It used to be much more complicated with coroutines magic, but this is definitely the safest and most efficient way of doing it without something like a compiler plugin. It's practically as safe as coroutine cancellation is, because coroutine cancellation uses a
CancellationException
, which RCE inherits from
👍 1
d
@Youssef Shoaib [MOD] Thank you - I suspected that was the case, but my understanding of the internals was not strong enough for me to be sure. Very helpful thank you color
@Riccardo Cardin Your articles are some of the most amazing technical deep-dives I've read. Unfortunately I'd only got part way through that one, and hadn't realised that it contained the answer I needed! Look forward to reading the rest!
gratitude thank you 1
🙌 1
r
No problem. I should have pointed you to the right point from the beginning 😅