andrzej
10/26/2023, 3:07 PMnull
without any additional burden, smth. like tapNone
in arrow.
Now I have to do the following:
someComplicatedComputation()
?: run {
println("We got null")
null
}
while for Option type in arrow I have much simpler and more readable:
someComplicatedComputation()
.tapNone {
println("We got null")
}
or did I overlook something from the standard lib?Joffrey
10/26/2023, 3:10 PMval result = someComplicatedComputation()
if (result == null) {
println("we got null")
}
It is as few lines as any of your options, and without any special operator or function.andrzej
10/26/2023, 3:14 PMfun calculateIt() =
someComplicatedComputation()
In order to apply your method I would need to change the form of all those functions to:
fun calculateIt() {
val result = someComplicatedComputation()
return result
}
which is not what I am looking for.Joffrey
10/26/2023, 3:19 PMcalculateIt
in the first place? If someComplicatedComputation
is meant to represent a chain of operations in real code (and not a function call), then injecting side effects in such chains is not very great looking either. It is nice (IMO) to see the imperative nature of the thing you're doing by separating the chain.Joffrey
10/26/2023, 3:22 PM.also {
if (it == null) println("we got null")
}
andrzej
10/26/2023, 3:46 PMWhat's the point in your first example if it's just one function calling directly another function? [...]this is not not relevant for my question 😕 As it comes to
also
- yes, it could be the solution but rather cumbersome. I don't want to run this check for all values, I just want to execute smth. for null
values. This is the reason why for nullable type we have ?:
operator (with your approach we could easily write at the end of each expression if == null
) and constructs like ?.let ... ?: ...
or why you have in some libs tapNone
methods (in Kotlin Arrow and in many other languages and libs), this is not my invention. I just don't understand why we don't have a standard method for such a basic and very useful construct. And if we don't have it in Kotlin, I suggest to add it. That's the reason why I wrote this post.Joffrey
10/26/2023, 3:53 PMI don't want to run this check for all values, I just want to execute smth. for null values.That doesn't really make sense. Even if a
tapNone
implementation was provided by the stdlib, it would have to check whether the value is null in order to decide whether to execute the block of code. So the if
check does happen on all values either way.Joffrey
10/26/2023, 3:53 PMJoffrey
10/26/2023, 3:53 PMI just don't understand why we don't have a standard method for such a basic and very useful constructI guess no compelling use case has ever been brought up. I for one never needed this. It would help to show a real-life example where you need this.
Joffrey
10/26/2023, 4:02 PMalsoIfNull
for consistency:
somethingNullable().alsoIfNull { println("foo") }
somethingNullable().also { if (it == null) println("foo") }
There is a big loss of generality with alsoIfNull
. Should we also add alsoIfNotNull
? What about alsoIfIsInstance<T>()
? I don't think saving a few characters justifies a new function over the general also
, unless the specific case occurs very often, or we can get some type inference benefit from it.Joffrey
10/26/2023, 4:05 PMandrzej
10/26/2023, 4:22 PMfunctions in which you want to inject imperative code in a way that still looks like an expression😱 Sorry, I feel we are not moving forward with this discussion. It's not about functional programming, it's not about complicated use cases, and all that stuff that you write about.... I provided the simplest possible use case (similar to code in many code bases) and let you know that other languages/libraries implement straightforward solution for this case (action on null for nullable types without result modification), also there are many similar questions on internet (e.g. https://stackoverflow.com/questions/45800036/how-do-i-run-a-block-of-code-if-a-nullable-type-is-null). Kotlin std lib has so many useful functions and constructs, hundreds of them I will never use, because I haven't found any good use case during many years of programming in Kotlin, yet such a useful function is not there. (I'd argue it is useful not only because myself and many other devs think that, but otherwise it wouldn't exist in other langs and libs, not necessarily functional)
Joffrey
10/26/2023, 4:48 PMawait
to call async functions vs Kotlin calling suspend functions like regular functions without await
keyword. This kind of properties of a language can drive what the stdlib really needs or not.
What matters more though is if people needed this a lot, which you did mention. It's hard to tell how many of the questions you are referring to would require a new stdlib function as you are suggesting or would be fine with just ?:
, also
, or regular if
statements.Klitos Kyriacou
10/26/2023, 5:08 PM// Usage: fun calculateIt() = someComplicatedComputation() ?: runAndReturnNull { ... }
public inline fun runAndReturnNull(block: () -> Any?): Nothing? {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
return null
}
Arjan van Wieringen
10/27/2023, 7:11 AMCharles Flynn
11/10/2023, 8:11 AM