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

allow implicit cast from union to its tag type #642

Closed
andrewrk opened this Issue Dec 4, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@andrewrk
Member

andrewrk commented Dec 4, 2017

  • implicit cast from tag type of union with comptime known value to union if the payload
    for that field is void
  • implicit cast from union with comptime known value to tag type if the payload for that field is void
const Letter = enum { A, B, C };
const Value = union(Letter) {
    A: i32,
    B,
    C,
};

fn foo(x: Letter) {

}

test "aoeu" {
    var x: Value = Letter.B;
    foo(x);
}

@andrewrk andrewrk added this to the 0.3.0 milestone Dec 4, 2017

@PavelVozenilek

This comment has been minimized.

PavelVozenilek commented Dec 4, 2017

What is the end goal for this construct (union with exposed tag)? Is it some kind of object support?
Something like Nim's "object variants"? ( https://nim-lang.org/docs/manual.html#types-object-variants )

Because I feel it is feasible to have typesafe union with simple syntax like:

const u = union {
  x : i32,
  y : this_type,
  z : that_type,
};

@andrewrk andrewrk added the accepted label Dec 6, 2017

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

add implicit cast from enum to union
when the enum is the tag type of the union and is comptime known
to be of a void field of the union

See #642

@andrewrk andrewrk changed the title from allow implicit casting between union and tag type when source value is comptime known and respective field is void to allow implicit cast from union to tag type when source value is comptime known and respective field is void Dec 6, 2017

@andrewrk andrewrk changed the title from allow implicit cast from union to tag type when source value is comptime known and respective field is void to allow implicit cast from union to its tag type Dec 6, 2017

@andrewrk andrewrk closed this in 2715f6f Dec 6, 2017

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