David Smith
06/17/2021, 1:37 PMval NullableData? = firstNullable?.let { first ->
secondNullable?.let { second ->
NullableData(first, second)
}
}
I have something like that but with many more fields so it is massively nested. I suppose I could turn nulls into exceptions and surround the whole thing in a try catch? In Haskell/Purescript/Scala we would have do
notation for this and I think arrow had done something like this but I couldn’t find it?Javier
06/17/2021, 1:43 PMDavid Smith
06/17/2021, 1:45 PMJavier
06/17/2021, 1:49 PMJavier
06/17/2021, 1:49 PMDavid Smith
06/17/2021, 1:49 PMDavid Smith
06/17/2021, 1:49 PMDavid Smith
06/17/2021, 1:49 PM!!
throws a null pointer?David Smith
06/17/2021, 1:50 PMfun <A> comprehend(f: () -> A): A? = try {
f()
} catch (e: NullPointerException) {
null
}
fun myFunction(json: ObjectNode): Pair<String, Percent>? = comprehend {
val a = json["name"].nullString!!
val b = json["allocated_percentage"].nullDouble?.toPercent()!!
a to b
}
Javier
06/17/2021, 1:50 PMJavier
06/17/2021, 1:50 PMDavid Smith
06/17/2021, 1:50 PMDavid Smith
06/17/2021, 1:50 PMJavier
06/17/2021, 1:51 PMDavid Smith
06/17/2021, 1:54 PMNir
06/17/2021, 1:55 PMNir
06/17/2021, 1:56 PMrunCatching { ... }.getOrNull()
Nir
06/17/2021, 1:56 PMNir
06/17/2021, 1:57 PMDavid Smith
06/17/2021, 1:59 PMDavid Smith
06/17/2021, 1:59 PMDavid Smith
06/17/2021, 2:00 PMrunCatching { something!! }.getOrNull()
David Smith
06/17/2021, 2:01 PMephemient
06/17/2021, 2:41 PMval nullableData: NullableData? =
if (first != null && second != null) {
NullableData(first, second)
} else null
smart-castingephemient
06/17/2021, 2:44 PMval nullableData: NullableData? = nullable {
NullableData(first.bind(), second.bind())
}
CLOVIS
06/17/2021, 7:14 PMdo
-notation equivalent, check out the #arrow library, they add Either and a direct equivalent to do
(the either
block)
In Kotlin, you should not use exceptions for business logic, for many reasons, one of them is how slow they are. https://elizarov.medium.com/kotlin-and-exceptions-8062f589d07 explains when you should and shouldn't use exceptions (written by Kotlin's lead designer)David Smith
06/17/2021, 7:47 PMdo
. The problem with arrow is that kotlin doesn’t have ad-hoc polymorphism so I can’t add the tools I need to the APIs I’m using, nullable types are “the kotlin way” and in the end @Javier and @Nir helped me achieve something similar to a Maybe monad. Just a shame you can’t generalize it over all monads in kotlin!CLOVIS
06/17/2021, 8:12 PMDavid Smith
06/18/2021, 9:28 AMnullable
in arrow just now however that requires suspended functions. I wondered what the performance cost is of something like runBlocking { nullable { … } }
?David Smith
06/18/2021, 9:29 AMCLOVIS
06/18/2021, 9:57 AMnullable.eager {}
(same syntax) that doesn't require the external function to suspend: https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core.computations/nullable/index.html#nullableDavid Smith
06/18/2021, 9:58 AMrunBlocking
over all my code though, I have a lot of code scraping a load of json so there’s loads of these functionsDavid Smith
06/18/2021, 9:58 AMelizarov
06/18/2021, 4:43 PMval nullableData: NullableData? = run {
val first = firstNullable ?: return@run null
val second = secondNullable ?: return@run null
// ... can do more, no further nesting
NullableData(first, second)
}
David Smith
06/18/2021, 4:44 PMnullable
and bind()
elizarov
06/18/2021, 4:45 PMDavid Smith
06/18/2021, 4:46 PMreaturn@run null
is doing?elizarov
06/18/2021, 4:46 PMDavid Smith
06/18/2021, 4:48 PMDavid Smith
06/18/2021, 4:49 PMnullable
although it’s nice that there is a kotlin way