-
-
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
non-exhaustive enums #2524
Comments
#3909 merged some related functionality as a property of
|
#3909 made all extern enums non-exhaustive. However, I don't think that should necessarily be how it is. Just because an enum is part of an ABI does not mean it is non-exhaustive. According to how the C ABI works, whether an enum is exhaustive or not is part of the documentation of the API. So #3909 solved a problem, but it also introduced a problem. And I do think that this proposal solves the new problem. With this proposal accepted, extern enums can be annotated as exhaustive or non-exhaustive. translate-c will choose non-exhaustive to be safe. One more part of the specification
I also want to include an extra feature of
Example: const E = enum {
a,
b,
_,
};
test "non-exhaustive switch" {
var e: E = .b;
switch (e) {
.a => {},
.b => {},
_ => {
// handle unnamed tag values
},
// no else required
// compile error will occur if another tag is added to E
}
switch (e) {
.a => {},
.b => {},
else => {}, // OK; no compile error if another tag is added to E
}
switch (e) { // error: switch not handling the tag `b`
.a => {},
_ => {},
}
switch (e) { // error: switch on non-exhaustive enum must include `else` or `_` prong
.a => {},
.b => {},
}
} I want to note that there is a case when you would do Related: #3991 |
A non-exhaustive enum is an enum that may have other unknown members.
This can help create APIs that may be extended in future. This sort of thing frequently comes up in data formats, network protocols, and even C APIs.
Possible syntax:
_
as a trailing field.Semantics:
enum(u5) { X, Y, Z, _ }
_
when an enum is already "full" (e.g. if it'su2
and you specify 4 values followed by a_
), then there is a compiler error.switch
-ed on, anelse
clause is always required@intToEnum
can never fail withhas no tag matching integer value
builtin.Enum
gains a new boolean fieldis_exhaustive
Related
The text was updated successfully, but these errors were encountered: