-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
std.enums: Add std.enums.CombinedEnums(Tagged)
functions
#21325
Conversation
|
||
/// Returns an enum containing the combined elements of the given enum types. | ||
/// The values are incremental integers starting at 0. | ||
fn CombinedEnums(comptime enum_types: anytype) type { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn CombinedEnums(comptime enum_types: anytype) type { | |
fn Combined(comptime enum_types: anytype) type { |
fn CombinedEnumsTagged(comptime tag: ?type, comptime enum_types: anytype) type { | ||
var fieldCount = 0; | ||
inline for (enum_types) |enum_type| { | ||
fieldCount += std.meta.fields(enum_type).len; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fieldCount += std.meta.fields(enum_type).len; | |
fieldCount += @typeInfo(enum_type).@"enum".fields.len; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like its meant to work on tagged unions as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my suggestion here was disagreeing. putting unions in and getting an enum out would lead to very confusing behavior to users imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless there was a strong motivating use case rather than this being suggested for std out of the blue
neither of the functions are marked |
std.debug.assert(@intFromEnum(Consumable.apple) == 0); | ||
std.debug.assert(@intFromEnum(Consumable.carrot) != 0); | ||
std.debug.assert(@intFromEnum(Consumable.chips) != @intFromEnum(Consumable.bread)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be using std.testing.expect
or expectEqual
in tests, assert
will cause the test to crash if it fails
Thank you but I don't want something like this in the standard library. This should be a separate package, if used at all. |
No description provided.