<https://twitter.com/vergauwen_simon/status/146823...
# arrow
s
👏 12
😍 11
👍 3
👍🏼 1
K 8
t
I think you may need to use absolute url here: https://nomisrev.github.io/continuation-monad-in-kotlin/guide/example/example-readme-03.kt it's 404 now.
also can you elaborate more on when to use Cont vs other type like Either. For me now it is kind of interchangeable with Either
s
Thanks @thanh, I fixed the links
👍 1
Oops, guess it deserves more explanation 😄 I plan to write something about a frequently asked question here which is about transactional database operations, and there
Cont
will also be useful.
TL;DR
Cont<E, A> <~~> suspend () -> Either<E, A>
It’s a bit like
IO
vs
Either
. When you receive an
Either
value the program has already run, but if you receive a
Cont
you can still control where the operation runs. I.e.
Copy code
// Exposed
suspend fun <E, A> Cont<E, A>.transaction(db: Database): Either<E, A> =
  newSuspendedTransaction(db, <http://Dispatchers.IO|Dispatchers.IO>) {
     // run the `suspend () -> E | A` by calling fold
     fold({ e -> rollback(); Either.Left(e) }, { a -> Either.Right(a) })
  }
💯 1
But it’s also useful for other applications, since it also serves as a lightweight runtime
🙌 1
So where in Scala where you might use `ApplicativeError`/
MonadError
to abstract over things like validation for
Either
,
Option
,
Validated
, ..
t
thanks @simon.vergauwen for the explanation. So
Cont
is a function and it is only evaluated to value by using
fold
function and some other similar functions like
toEither
.
I'm using similar strategy for the transaction with
Either
( copy from your gist about Sqldelight Transaction) which works great, but it seems a little bit better with
Cont
.
s
That’s 100% correct @thanh! You’re welcome 🙂 I’m probably going to write a bit about this next, and how you can use this for SqlDelight/Exposed (which can be translated to all frameworks suspend/non-suspend).
💯 3
t
Thanks for the great work Simon! it seems it opens a lot of possibilities.
🙌 1
i
Great stuff Simon. It looks like a good abstraction to me! Examples you provide on the blog are also very valuable. Thanks for the great work!
🙌 1
s
Thanks for the kind words @Ivan Dugalic! Very happy you like it, more similar stuff coming soon 🙂
🤩 1