Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

yyny
Copy link
Contributor

@yyny yyny commented Sep 6, 2024

No description provided.


/// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn CombinedEnums(comptime enum_types: anytype) type {
fn Combined(comptime enum_types: anytype) type {

Avoid Redundant Names in Fully-Qualified Namespaces

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fieldCount += std.meta.fields(enum_type).len;
fieldCount += @typeInfo(enum_type).@"enum".fields.len;

Copy link
Contributor

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

Copy link
Contributor

@nektro nektro Sep 6, 2024

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

Copy link
Contributor

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

@xdBronch
Copy link
Contributor

xdBronch commented Sep 6, 2024

neither of the functions are marked pub

Comment on lines +1571 to +1573
std.debug.assert(@intFromEnum(Consumable.apple) == 0);
std.debug.assert(@intFromEnum(Consumable.carrot) != 0);
std.debug.assert(@intFromEnum(Consumable.chips) != @intFromEnum(Consumable.bread));
Copy link
Contributor

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

@andrewrk
Copy link
Member

andrewrk commented Sep 6, 2024

Thank you but I don't want something like this in the standard library. This should be a separate package, if used at all.

@andrewrk andrewrk closed this Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants