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

proposal: ability to specify tag type of enums, and set the tag value #305

Closed
andrewrk opened this issue Apr 5, 2017 · 4 comments
Closed

proposal: ability to specify tag type of enums, and set the tag value #305

andrewrk opened this issue Apr 5, 2017 · 4 comments

Comments

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Apr 5, 2017

Example:

const u4 = @intType(false, 4);
const VGAColor = enum(u4) {
    Black = 0,
    White = 3,
    Blue = 10,
};

const VGAEntry = packed struct {
    c: u8,
    fg: VGAColor,
    bg: VGAColor,
};

So here we set the tag type of VGAColor to u4. Then we set the tag values for Black, White, and Blue. This would also work even if an enum value had a payload:

const Foo = enum(u8) {
    One = 10,
    Two: bool = 13,
};

The tag type has to support equality and comparison operators, and there's no operator overloading. So it pretty much limits it to integers.

Delete the ability to explicit cast enum tags to any integer and replace it with the ability to only explicitly cast to the tag type. So if you want to cast an enum tag to an integer, you have to specify the tag type.

@AndreaOrru
Copy link
Contributor

@AndreaOrru AndreaOrru commented Apr 5, 2017

+1

Loading

@andrewrk andrewrk added this to the 0.2.0 milestone Apr 6, 2017
@raulgrell
Copy link
Contributor

@raulgrell raulgrell commented Apr 11, 2017

I agree, it's a nice feature with intuitive syntax.

Loading

@skyfex
Copy link

@skyfex skyfex commented Nov 29, 2017

I would love to see this implemented soon. It would make Zig great for programming microcontrollers.

Loading

andrewrk added a commit that referenced this issue Dec 1, 2017
@andrewrk
Copy link
Member Author

@andrewrk andrewrk commented Dec 1, 2017

You can now specify the tag type in enums, as described in this issue. You can also access the tag type with @EnumTagType(EnumType). However these things are left to do before the issue is complete:

  • allow enums with specified integer sizes in packed structs
  • support setting the integer tag value of enum fields as described
  • Delete the ability to explicit cast enum tags to any integer and replace it with the ability to only explicitly cast to the tag type.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants