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
Labels
accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
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 Solving this issue will likely involve adding new logic or components to the codebase. label Apr 5, 2017
@AndreaOrru
Copy link
Contributor

+1

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

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

@tiehuis tiehuis added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Sep 15, 2017
@skyfex
Copy link

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
Copy link
Member Author

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 andrewrk added the accepted This proposal is planned. label Dec 2, 2017
The-King-of-Toasters added a commit to The-King-of-Toasters/zig that referenced this issue Mar 16, 2024
Fixes a TODO referencing the ancient issue ziglang#305.
The-King-of-Toasters added a commit to The-King-of-Toasters/zig that referenced this issue Mar 16, 2024
Fixes a TODO referencing the ancient issue ziglang#305.
RossComputerGuy pushed a commit to ExpidusOS-archive/zig that referenced this issue Mar 20, 2024
Fixes a TODO referencing the ancient issue ziglang#305.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

5 participants