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

Comments

Projects
None yet
5 participants
@andrewrk
Member

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.

@andrewrk andrewrk added the enhancement label Apr 5, 2017

@AndreaOrru

This comment has been minimized.

Show comment
Hide comment
@AndreaOrru
Member

AndreaOrru commented Apr 5, 2017

+1

@andrewrk andrewrk added this to the 0.2.0 milestone Apr 6, 2017

@raulgrell

This comment has been minimized.

Show comment
Hide comment
@raulgrell

raulgrell Apr 11, 2017

Contributor

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

Contributor

raulgrell commented Apr 11, 2017

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

@skyfex

This comment has been minimized.

Show comment
Hide comment
@skyfex

skyfex Nov 29, 2017

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

skyfex commented Nov 29, 2017

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

andrewrk added a commit that referenced this issue Dec 1, 2017

@andrewrk

This comment has been minimized.

Show comment
Hide comment
@andrewrk

andrewrk Dec 1, 2017

Member

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.
Member

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.

andrewrk added a commit that referenced this issue Dec 2, 2017

@andrewrk andrewrk added the accepted label Dec 2, 2017

@andrewrk andrewrk closed this in 137c8f5 Dec 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment