Skip to content

Proposal: Replace _ with unused keyword #10245

@SpexGuy

Description

@SpexGuy

Zig currently uses _ in five places:

  • marking a parameter as unused (fn foo(_: u32) void)
  • marking an expression value as unused (_ = func();)
  • marking a named variable as unused (_ = variable;)
  • marking an enum as extensible (enum (u8) { _ })
  • declaring a padding field (_: u6,)

This proposal is to replace the first three uses with a new keyword, unused. It behaves as follows:

  • On parameters, fn foo(unused index: u32) void

Allowing a name here can improve documentation, and make it easier to notice if the parameter is actually needed. However, sometimes names are not needed, so we could also allow the syntax fn foo(unused: u32) void. Since unused is a keyword, this is not shadowing and can be properly identified by the parser.

  • On locals, unused can be used in the place of const or var. As in,
unused result = func(); // result can be named for better documentation
unused = func(); // name is optional
unused result: u32 = func(); // type annotations are also allowed
unused: u32 = func(); // even without a name

unused = variable; // this could be a compile error if `variable` is a local or parameter, unused should instead be placed on the declaration of variable.

As you might expect, attempting to use a variable declared as unused is a compile error.

This change is breaking, but zig fmt would be able to perform most of the update correctly.

After this change, _ would be used exclusively for extensible enums and padding fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingImplementing this issue could cause existing code to no longer compile or have different behavior.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions