-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Change meaning of underscore from actual identifier name to ignoring the value being named #4164
Comments
Main proposal - accepted Other ones- not. types are needed to make sense of whether functions are compatible with each other. The inference required to do this proposal is complicated and not beneficial enough to be worth it.
We have |
So Zig already has special handling of |
There's an accepted proposal for non exhaustive enums: (#2524), which would mean you couldn't use it to pad enums:
|
If #4859 is accepted and |
This should also work for struct and union fields: test "" {
const S = struct {
a: u32,
// These fields cannot be assigned to or accessed.
// They only serve as padding.
_: u32,
_: u32,
b: u32,
};
var s =S{
.a = 1,
.b = 2,
};
} This does not apply to enums where you can skip values by explicitly assigning the tag value. |
I just wanted to add, since it's not clear from the above example, that default assignment must remain legal here and highly recommended. For example, if dealing with some opaque C API: const Flags = packed struct(u32) {
_: u16, // Presumably this memory is undefined, this would be a potential footgun
some_flag: u1 = false
_: u15 = 0, // Default assignment must remain legal for unnamed fields
};
extern fn some_c_api(flags: Flags) void; // We might not know how a library will treat the underlying integer
test {
some_c_api(.{ .some_flag = true });
} |
Main Proposal
This is a play stolen from functional languages like Haskell and OCaml (probably a bunch of other languages as well) where parameters to functions can be ignored by placing
_
where the parameter name be. The need/usefulness for this comes about when using function pointers; the function's signature may not be ideal for that particular function and may include parameters that the function does not need to used. The motivating use-case was when providing callback functions for wayland input events.Under this proposal, an underscore can appear in the same places that identifier introduction occurs (i.e. variable declarations, for loop variables, function parameters, etc.).
I believe this would not break any existing programs, since this proposal only expands where the underscore can be used. However, instead of only being able to use it for one parameter of a function, it could potentially be used for all of them.
==== Rest of proposal rejected ====
Debatable additional part
The first part above seems to me to be very uncontroversial and almost objectively good for the language. This second part seems to me equally reasonable, but I would be less surprised with disagreement.
For places where a type annotation is expected (like function parameters), if the variable is ignored with the underscore, then the type should be unnecessary as well. With both parts of this proposal, I could change this empty function from
to
Not only is this easier to type, I believe it makes the function more obviously trivial.
A more debatable third part
This last part is less motivated by a use-case and more motivated by making the language beautiful (which understandably may not fit into the language philosophy). Nevertheless, I do think the feature would have legitimate uses.
All of the parts together would say this:
unreachable
)Above three statements seem to me to be rather symmetric and easy-to-understand.
For assignments like
var abc = 5 + _;
this is not very useful, but while calling functions from an unfamiliar API during prototyping, it may be useful to not need to have all the parameters that the function asks for. For example, the admittedly contrived functionCould be called like
addOrIdentity(false, 5, _);
because it is known at compile-time that the value fory
is not going to be used. Note that you can always pass underscore as a parameter which was defined with an underscore. That is:What are thoughts? The three sections are in the order that I think they are useful and good.
The text was updated successfully, but these errors were encountered: