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

Field access of explicit tag type union should prioritize definitions over tag. #959

Open
alexnask opened this Issue Apr 27, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@alexnask
Contributor

alexnask commented Apr 27, 2018

Example:

const Id = enum {
    A, B,
};

const Foo = union(Id) {
    A: A,
    B: B,

    const A = u8;
    const B = void;
};

test "union" {
    assert(Foo.A == Id.A); // This should probably return 'u8' instead of Id.A
}

Also, there should probably be some way to refer to definitions with the same symbols as the enum tags even in auto-generated tag unions (and no-tag unions), or be made a compile error (the current state where there are unreachable definitions is the problem imo).

@alexnask alexnask changed the title from Field access of explicit tag union should prioritize definitions over tag. to Field access of explicit tag type union should prioritize definitions over tag. Apr 27, 2018

@alexnask

This comment has been minimized.

Contributor

alexnask commented Apr 28, 2018

@andrewrk

In my opinion the sane thing to do is disallow accessing the tag value through the union type for explicit tag type unions.

For implicit (or extern) tag type unions, perhaps we should disallow definitions that shadow the tag name?

@andrewrk andrewrk added the proposal label Apr 28, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Apr 28, 2018

@andrewrk andrewrk added the accepted label May 7, 2018

@andrewrk andrewrk closed this in 77a1a21 May 7, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented May 7, 2018

For now I made a change to prioritize members over tag values for tagged unions. For implicit tagged unions, you can use @TagType(the_union).Foo to access tag values if for some reason the name is shadowed.

To finish implementing the rest of this proposal:

  • add compile error for bare enums when member shadows enum tag name
  • add compile error for exern union when member shadows union field name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment