Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better convention for ignoring function params #9239

Closed
fengb opened this issue Jun 25, 2021 · 4 comments
Closed

Better convention for ignoring function params #9239

fengb opened this issue Jun 25, 2021 · 4 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@fengb
Copy link
Contributor

fengb commented Jun 25, 2021

When working with function pointers, it is often necessary to conform to an existing signature without needing to use the parameters.

Currently we have 2 options:

fn foo(_: []const u8, _: []const u8) void {
}

There's a few problems with this approach:

  1. Declares a real _ variable that shadows the throwaway variable. Possibly resolved by #1802
  2. Hides the intent of the variable. In more generic codebases like build tools, this often becomes a problem when argument names are not readily available.

Another approach is explicit ignoring:

fn foo(name: []const u8, signature: []const u8) void {
    _ = name;
    _ = signature;
}
  1. If this variable is used later, this hint is now misleading. Fixed by #9238
  2. If there are lots of unused arguments, there will be lots of superfluous lines. This hides intent instead of exposing it, especially if some arguments are used and it takes a matching game to locate the specifics.

Solution

I propose a new convention to solve both usecases of documenting the consumer behavior and indicating non-usage:

fn foo(_name: []const u8, _signature: []const u8) void {
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jun 25, 2021
@andrewrk andrewrk added this to the 0.9.0 milestone Jun 25, 2021
@andrewrk
Copy link
Member

I veto identifier names having any semantic meaning.

@ghost
Copy link

ghost commented Jun 26, 2021

I veto identifier names having any semantic meaning.

Then how about this:

fn foo(_(name): []const u8, _(signature): []const u8) void {}

That is, once _ becomes a keyword, allow it to take an optional name in parentheses for purposes of documentation and introspection.

@ekipan
Copy link

ekipan commented Jul 28, 2021

Currently _ is permitted as a parameter name. This seems like a bug, since you can only use it once and it cannot be referred to within the function.

Personally I'm not convinced having a name for an unused parameter that you have to _ = name is useful enough to tolerate the potential bugs, but unless that changes perhaps _ in a parameter list should be a compile error.

@bb010g
Copy link

bb010g commented Dec 9, 2021

#1802 (comment) (and #6062) establish that _, an identifier name, has a semantic meaning of "discard" when written without raw identifier syntax, i.e. @"_" does not have that special semantic meaning. _ is also not a keyword.

_foo could also have a semantic meaning of "discard" due to its being prefixed by _, and @"_foo" could not have that special semantic meaning, just like with _. _foo would also not have to be a keyword. If this was implemented, _ becomes the shortest example of the rule that non-raw identifiers with a first byte/character of _ invoke discard semantics, while raw identifier syntax (@"_") avoids invoking discard semantics. There will still only be one rule to learn for identifiers that mean discard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

4 participants