-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Shortcut (type inferrence) for naming enum values #683
Copy link
Copy link
Closed
Labels
acceptedThis proposal is planned.This proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.docsproposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Milestone
Metadata
Metadata
Assignees
Labels
acceptedThis proposal is planned.This proposal is planned.contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.This issue is limited in scope and/or knowledge of Zig internals.docsproposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Current Progress
Problem
In Zig we currently have to type out the full "path" to an enum value. I.e., for the following enum:
We have to provide the namespace (if relevant), the enum and the value name:
someNamespace.Type.OkorType.OkThis can get very tedious. This is en example from my testing:
The code can feel overly verbose and repetitive. It also discourages use of enums. People might use integers or booleans instead of a descriptive enum.
Proposal
Whenever Zig can infer the enum type from the context of the code, it should. Instead of writing
Type.OK, you can just typeOKor.OK(one or the other, which one is up for debate)Examples
When declaring a const or variable, you still need the full name:
var foobar = myModule.Type.OkWhen assigning to an already declared varible, you can use the short form:
foobar = .OkWhen assigning to a field in a struct, or instantiting a struct , you can use the short form:
It should also be possible to use with these proposals: #661 and #649
Discussion
Pros:
Cons:
baz(Active, Enabled, On)is not much more helpful thanbaz(true,true,true))A related idea would be to infer namespace names for other things too (like function calls). This should probably be a separate proposal.
Edit: Changed the examples from
Okto.Oksyntax