Luca Piccinelli
03/23/2021, 6:51 AMResult
type out-of-the-box is great as well. What I don't like about those features, is that it is not provided a clean way to compose many nulls
or many Result
.
That's the reason why I came out with Konad library. https://github.com/lucapiccinelli/konad
Instead of composing nulls like the following
val foo: Int? = 1
val bar: String? = "2"
val baz: Float? = 3.0f
fun useThem(x: Int, y: String, z: Float): Int = x + y.toInt() + z.toInt()
val result1: Int? = foo
?.let { bar
?.let { baz
?.let { useThem(foo, bar, baz) } } }
// or
val result2: Int? = if(foo != null && bar != null && baz != null)
useThem(foo, bar, baz)
else null
you have an API like
val result: Int? = ::useThem.curry()
.on(foo.maybe)
.on(bar.maybe)
.on(baz.maybe)
.nullable
or even with error messages
val result: Result<Int> = ::useThem.curry()
.on(foo.ifNull("Foo should not be null"))
.on(bar.ifNull("Bar should not be null"))
.on(baz.ifNull("Baz should not be null"))
.result