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

@enumToInt should work on union(enum) #1711

Closed
thejoshwolfe opened this Issue Nov 9, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@thejoshwolfe
Copy link
Member

commented Nov 9, 2018

const std = @import("std");
const Foo = union(enum).{
    A: bool,
    B: u8,
    C,
};

test "asdf" {
    const a: Foo = Foo.{ .A = true};
    std.debug.assert(@enumToInt(a) == 0);
}
$ zig test asdf.zig 
asdf.zig:10:33: error: expected enum, found type 'Foo'
    std.debug.assert(@enumToInt(a) == 0);
                                ^

Alternatively, is it possible to isolate the tag part of a union(enum), which I believe is an enum? I couldn't find an answer to this in the docs.

@thejoshwolfe thejoshwolfe changed the title @enumToInt should work on union(enum) `@enumToInt` should work on `union(enum)` Nov 9, 2018

@thejoshwolfe thejoshwolfe changed the title `@enumToInt` should work on `union(enum)` @enumToInt should work on union(enum) Nov 9, 2018

@thejoshwolfe

This comment has been minimized.

Copy link
Member Author

commented Nov 9, 2018

Looks like this is the answer. Replace @enumToInt(a) with @enumToInt(@TagType(@typeOf(a))(a)). Doing a cast to @TagType(Foo) is how you isolate the tag value.

Still seems like @enumToInt(a) should just work though.

@andrewrk andrewrk added this to the 0.4.0 milestone Nov 9, 2018

@andrewrk andrewrk added the proposal label Nov 9, 2018

@tgschultz

This comment has been minimized.

Copy link
Contributor

commented Nov 9, 2018

For the record, @enumToInt(@TagType(@typeOf(a))(a)) can be shortened to @enumToInt(meta.activeTag(a)).

@thejoshwolfe

This comment has been minimized.

Copy link
Member Author

commented Nov 9, 2018

Ah. I forgot to check the meta library. Thanks.

@andrewrk andrewrk added the accepted label Nov 21, 2018

emekoi added a commit to emekoi/zig that referenced this issue Feb 10, 2019

@andrewrk andrewrk closed this in 9b3013d Feb 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.