If I want to fold with a curried suspend function,...
# arrow
e
If I want to fold with a curried suspend function, what's the right way to do it? Is it possible to inline it all the way?
This can't be used, for instance
Copy code
fun curry(x: Int): suspend (Int) -> Unit = {
  suspendPrint(x + it)
}
image.png
y
Just FYI it won't be inlined. I believe you can do
{ f(it) }
e
Thanks, yes that works but gets quite ugly when we need to do the currying at the callsite 😞
Copy code
{ logAndNack(logger, message)(it) },
y
That's why Kotlin isn't really intended to do currying. Returning a function type is rarely idiomatic in Kotlin. Instead, what you wanna do is just have logAndNack take all the needed arguments
e
Right, that's way more readable. 🙂 Thanks!
y
If you want it to be nicer, then with context receivers (which are currently experimental, so not the best idea for production) you can turn the logger into a context receiver, so the call would just be
logAndNack(message, it)
which is even nicer. In general, if you find yourself repeating a set of parameters pretty often, it's time to consider if they'd work as context receivers instead. This eliminates a good chunk of use-cases for currying.
e
we are using it, and have multiple receivers at this callsite even 😄
y
Then yeah, experiment with turning logger into a context, and perhaps even message if it's repeated and makes sense
e
will consider adding the logger into the context 🙂
s
That's why Kotlin isn't really intended to do currying.
100% this, although I've request several times to allow calling
suspend
lambdas where
inline
lambdas are allowed not sure if this ever made it into a YouTrack issue 🤔
e
If you end up creating an issue, feel free to link it so I can add a vote 😄