is there an `option { }` continuation for nullable...
# arrow
k
is there an
option { }
continuation for nullable types similar to
either {}
?
r
Hi @kioba, not yet but we have built it twice before based on arrow-continuations. Pretty easy if you want to contribute it for the next release. We were planning to tackle it when we redo all comprehensions to remove the reflection trick and use arrow-continuations instead.
👍 1
k
I would be happy to contribute that to the next release! can you point me to a lead where should be start looking? I assume it is
either{}
implementation
👌 1
i
This is a Quick'n Dirty implementation:
Copy code
import arrow.core.None
import arrow.core.Option
import arrow.core.Some

private object NoneException : RuntimeException()
class OptionDsl {
    fun <T> Option<T>.bind(): T =
        if (this is Some) this.t
        else throw NoneException
}

fun <T> option(block: OptionDsl.() -> T): Option<T> =
    try {
        OptionDsl().block().let(::Some)
    } catch (_: NoneException) {
        None
    }
r
@kioba it has to be done with reset / shift
👍 2
From arrow-continuations
@ilaborie thanks, with arrow Continuations you should be able to use shift to short-circuit without exception in suspend
Also the new binding syntax in the next release with the new Continuations is using the operator invoke
@kioba the arrow Continuations test have example of reset and shift which you can combine with a DSL similar to @ilaborie example but based on the receiver being the DelimitedScope<A?>
If you need help I'm happy to do a quick demo on how Continuations work or explain any internals until we have docs for that module.
There are two impl of delimscope. Nullable types only need single shot
Multishot is for list, streams and others that bind more than once
i
Great, I'm looking forward to have a cleaner solution, without the dirty Exception tricks
k
spent the last two days on discovering
DelimitedScope<A?>
with shift and how it works under the hood in
arrow-continuations
. Am I correct that we are not able to get rid of the
BindingSyntax
with `DelimitedScope` for some reason I was under the impression that it will be possible in some ways but probably we would need meta for that 😅
r
We can get rid of it all around afaik. Do you have an example where it's needed?. Bind should be able to be implemented specialled for nullable types with shift { null }