Equivalent to `@discardableResult` for function parameters?

@discardableResult allows you to not have to write _ = is there something similar for arguments?

I have an often used function typealias that has one function parameter:

/// Some action buttons are given a dismiss function you can execute inside of your action.
/// This will allow you to dismiss whatever View is currently showing the action.
public typealias DismissFn = () -> Void

/// A generic action that happens when you press a button
public typealias MyAction = (DismissFn?) -> Void

However, everytime I use the function I have to write { _ in print("using MyAction") } or else I get:

Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored

It would be cool if we could add a @discardableParameters to the function that we don't need to write _ in everywhere and can just write { print("...") }

If not I think this could be a great addition!

1 Like

@ignorableArgument?

Would that be limited to single arguments, as in your example?

This reminds me of talking to other people after they’ve decided not to pay attention.

Discardable result seems like a different idea, although not very.

Interesting thought, cheers.

You might be able to solve this problem with overloading. For example, withExtendedLifetime has two overloads: one that passes an argument into the closure, and one where no arguments are passed into the closure. This means that if you don't need the argument, you can still call it without writing _ in.

1 Like