KMP-NativeCoroutines icon indicating copy to clipboard operation
KMP-NativeCoroutines copied to clipboard

Add @discardableResult attribute/s

Open thomasflad opened this issue 9 months ago • 7 comments

Speaks something against adding the @discardableResult attribute to functions like asyncFunction(for:). Sometimes we just want to ignore the result/return value

thomasflad avatar Apr 29 '24 10:04 thomasflad

I'd rather prefer to have the warning, can't you do _ = asyncFunction(for:)? Which is the suggested syntax for this anyway.

vanniktech avatar Apr 29 '24 13:04 vanniktech

@thomasflad do you have some examples of cases where you would discard the result? I am not sure if we are going to find a way (other than ignoring all results) that is shorter than _ =, but who knows.

rickclephas avatar Apr 29 '24 19:04 rickclephas

Thanks for your comments @vanniktech @rickclephas

We have situations where we don't care about the Result of the suspend function call. Can you explain why do you think it's the suggested syntax to use _ =? We are calling asyncFunction(for:) only for the side effect and not for the result so I think it should be optional to consume the result. Wdyt?

thomasflad avatar May 02 '24 14:05 thomasflad

Better to be explicit and state with _ = that you don't care whether someone new to the codebase not getting a warning because he didn't know that the return had to be handled.

vanniktech avatar May 02 '24 14:05 vanniktech

This makes sense when the return has to be handled, but this is not the case in my opinion. When the suspend function is only triggering a side effect and is returning Unit, why is it mandatory to handle the result? When the suspend function is returning a value, then the developer is usually interested in this return value.

thomasflad avatar May 02 '24 16:05 thomasflad

And by adding @discardableResult you're enforcing everyone to not care.

vanniktech avatar May 03 '24 11:05 vanniktech

I don't think we should add @discardableResult for all cases. Swift obviously thought about this and decided to show warnings and require developers to be explicit about their intentions.

We might however be able to do something about the Unit return type. That would normally be Void and wouldn't require the result to be handled in Swift.

I'll take a look and see if we can somehow make an exception for the Unit return type.

rickclephas avatar May 03 '24 11:05 rickclephas