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

support tagged unions #5

Closed
7 of 8 tasks
andrewrk opened this issue Dec 11, 2015 · 2 comments
Closed
7 of 8 tasks

support tagged unions #5

andrewrk opened this issue Dec 11, 2015 · 2 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Dec 11, 2015

  • - enums where all types are void (or left unspecified) codegen to a simple unsigned integer with the smallest bit width possible.
  • - support enums with values attached
  • - implement #enum_count(...)
  • - exported enums
  • - pub enums
  • - fix debug offsets so that union values display correctly
  • instead of function invocation to create an enum value with a payload, Foo.One { Point { .x = 1, .y = 2,} }
  • comparison operators shouldn't work for enums with payloads
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Dec 11, 2015
@andrewrk
Copy link
Member Author

Proposal:

enum Foo {
    One {
        x: i32,
    },
    Two {
        x: u64,
        y: u64,
    },
    Three,
}

enum Bar {
    A = 0,
    B = 2,
    C = 4,
    D = 8,
}

fn f() {
    const foo = Foo::One { .x = 1234, };
    const bar = Bar::A;

    if (#enum_count(Foo) != 3) {
        unreachable;
    }

    if (#enum_count(Bar) != 4) {
        unreachable;
    }

    if (#sizeof(Foo) != 17) {
        unreachable;
    }
    if (#sizeof(Bar) != 1) {
        unreachable;
    }
}

@andrewrk
Copy link
Member Author

New plan:

struct Point {
    x: u64,
    y: u64,
}

enum Foo {
    One: i32,
    Two: Point,
    Three: void,
}

enum Bar {
    A,
    B,
    C,
    D,
}

fn f(foo: Foo) -> void {
    switch (foo) {
        one(x) {

        },
        two(point) {

        },
        three {

        },
    }
}


fn f() {
    const foo1 = Foo.One(13);
    const foo2 = Foo.Two(Point { .x = 1234, .y = 5678, });
    const bar = Bar.A;

    if (#enum_count(Foo) != 3) {
        unreachable;
    }

    if (#enum_count(Bar) != 4) {
        unreachable;
    }

    if (#sizeof(Foo) != 17) {
        unreachable;
    }
    if (#sizeof(Bar) != 1) {
        unreachable;
    }
}

No longer allowed to assign to enum values. They're always ordered with 0 as the first one, N - 1 as the last one, and #enum_count(...) gives you N.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

1 participant