Passing in a function only works if inlined, not if created by higher-order function

Viewed 39

Two methods were sharing the exact same logic for handling a function returning a CompletableFuture, so I thought I would extract it into a common function that would return this logic.

Extracting the main body of the method into a common function works fine: extracted All breakpoints are hit and the code works.

I then extracted this closure creation into a separate function, myFunction, which is basically returning a function that matches what the whenComplete signature requires.

        }.whenComplete(myFunction(ctx, payload))
    }

    private fun myFunction(
        ctx: ChannelHandlerContext,
        payload: DataReport
    ) = { result:SaveDataReportResult, throwable: Throwable? ->
        extracted(throwable, result, ctx, payload)
    }

After doing this, none of the breakpoints of extracted are hit and the body of the method is not executed (as in no logger statements are being exercised).

This is quite counter-intuitive, so what am I missing here? I ran this by a colleague, and he could also not fault the code. Not all that familiar with Kotlin, I have to admit, so there might be some details I have missed.

0 Answers
Related