Infix functions' dot chaining


#1

There is my code:

class AsyncProcessor(private val task: suspend CoroutineScope.() -> Unit) {
    infix fun handleError(errHandler: (Throwable) -> Unit): AsyncProcessor {
        ...
        return this
    }
    fun run(composite: AsyncBag): Job = ...

Here I return ‘this’ from handleError() in order to chain it with run() like that:

AsyncProcessor {
    ...
} handleError {
    ...
}.run(bag)

But turns out it can’t be done. Albeit, if i mark run() as infix, it can be achieved in the following way:

AsyncProcessor {
    ...
} handleError {
    ...
} run (bag)   

If I’m doing everything right, it seems that even if an infix function returns this, it cannot be chained with an ordinary function

Why is that?


#2

I am guessing because of precedence. Try putting the expressions into parenthesis, and it might start working.


#3

Yeah, you are right, now it works. Even though looks quite strange
Thanks! )