Hi Guys! I am wondering is there any generic funct...
# arrow
v
Hi Guys! I am wondering is there any generic function in arrow that helps turn a bunch of nullable values into a one nullable value given a function e.g. data class constructor, it could be smth like this:
Copy code
fun <A, B, C, D> fromNullable(a: A?, b: B?, c: C?, f: (A, B, C) -> D): D?
I do understand that it could be done by Option monad comprehension, but maybe there is a way for basic nullable types from kotlin?
s
There is no way of doing this atm, but I actually want to add it to Arrow-Core. Especially given we’re deprecating
Option
in
0.11.0
. https://github.com/arrow-kt/arrow-core/issues/120
I actually needs this relatively frequent, and end up chaining
?.let
which is super ugly.
1
It would great to have your input/suggestions on the ticket if you don’t mind :)
v
@simon.vergauwen same for me,
?.let
just annoys me. I'll give a look at the ticket, tnx!
👍 1
s
We wrote a bunch of overloaded
let
scoping functions for this:
Copy code
fun <A: Any,B: Any,C: Any,R> let(a: A?, b: B?, c: C?, block: (A, B, C) -> R): R {
    return if (a != null && b != null && c != null) block(a,b,c) else null
}
s
Yes, we need to expose something similar from Arrow IMO, or even Kotlin std.
v
@streetsofboston I did the same functions in my codebase, but it seems like we could have them on lib level
2
feels like a common pattern
s
Maybe it shouldn’t be called
let
, but
guard
or something similar 🙂
v
@simon.vergauwen would you mind if I create a separate ticket for this issue?
s
Not at all!
v
ok, cool
s
Thanks for the ticket! 👍
🤝 1
v
Posting the issue here, so anyone who is interested could participate https://github.com/arrow-kt/arrow-core/issues/159