Can I force the caller of a function to process the return value?

I’m funneling exceptions through a result type, but this funneling is not of much use if the caller can ignore the result value of the funnel function. Is it possible to define a function such that the code won’t compile unless it is explicitly assigned to a variable? Example:

fooBar() // this won't compile
val r = fooBar() // this will compile

I don’t think there a way to do that now, but even if there is, nothing prevents the caller to just ignore that value.
You might need to use some code analyzer to catch this. For Java there’s CheckReturnValue I don’t know if it works for kotlin.

1 Like

There are several requests for that, see this issue https://youtrack.jetbrains.com/issue/KT-18024 and the related ones.

1 Like