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

add implicit cast for enum literal to optionals and error unions #2811

Closed
tiehuis opened this issue Jul 4, 2019 · 4 comments
Closed

add implicit cast for enum literal to optionals and error unions #2811

tiehuis opened this issue Jul 4, 2019 · 4 comments
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@tiehuis
Copy link
Member

tiehuis commented Jul 4, 2019

For example, the following does not compile today:

const Foo = enum {
    A,
    B,
};

pub fn main() void {
    var foo: ?Foo = undefined;
    foo = .A;
}

The error is:

zig run t.zig 
/tmp/t.zig:8:11: error: expected type '?Foo', found '(enum literal)'
    foo = .A;

For this to work today you need to specify the enum type with foo = Foo.A.

This is simply proposing to check the inner type of a nullable in the compiler and support performing enum literal inference based on that.

@tiehuis tiehuis added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 4, 2019
@andrewrk andrewrk added the accepted This proposal is planned. label Jul 4, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Jul 4, 2019
@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. stage1 The process of building from source via WebAssembly and the C backend. labels Jul 4, 2019
@andrewrk
Copy link
Member

andrewrk commented Jul 4, 2019

The implementation should also support implicit cast to error unions.

@andrewrk andrewrk changed the title Support enum literal assignment to nullables add implicit cast for enum literal to optionals and error unions Jul 4, 2019
@FireFox317
Copy link
Contributor

@andrewrk, Should it only be allowed for error unions? And not for error sets? Because my current implementation in #2867 does casting for both error unions and error sets.

@andrewrk
Copy link
Member

Yeah, not error sets. That's a safeguard against a footgun because if you have .foo casting into E!T where:

const E = error {foo};
const T = enum {foo};

Which one should it cast to? It's ambiguous.

@andrewrk
Copy link
Member

andrewrk commented Sep 4, 2019

Fixed by @FireFox317 in #2867, landed in e540e5b.

@andrewrk andrewrk closed this as completed Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants