Skip to content

allow use of undefined as switch key value #2798

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

Closed
shawnl opened this issue Jul 1, 2019 · 4 comments
Closed

allow use of undefined as switch key value #2798

shawnl opened this issue Jul 1, 2019 · 4 comments

Comments

@shawnl
Copy link
Contributor

shawnl commented Jul 1, 2019

switch (foo) {
undefined => do_stuff(),
else => do_other_stuff(),
}

While would trim the undefined key branch as unreachable.

Why is this useful? you might ask. It allows more ways to disable code paths based on compile time variables. undefined here would generally be a const. Instead you currently get

    #1 with zig trunk
    zig trunk (Editor #1, Compiler #1) Zig

/home/ubuntu/example.zig:4:5: error: use of undefined value here causes undefined behavior

Compiler returned: 1

Which is bogus (however llvm won't take an undefined here either, IIUC, so it does have to be explicitly handled).

Related: #597

@shawnl
Copy link
Contributor Author

shawnl commented Jul 1, 2019

I'm starting to have second thoughts about this proposal, as it muddles the concept of undefined. (not the C concept, but the Zig concept) However I think my use case is sound. Any suggestions?

@daurnimator
Copy link
Contributor

switching on undefined should immediately be an error: undefined means "any value could be here; it's an error to try read it"

@shawnl
Copy link
Contributor Author

shawnl commented Jul 1, 2019

@daurnimator yeah, but it also turns into unreachable in a number of places in llvm, except that zig doesn't let it do that.

@shawnl
Copy link
Contributor Author

shawnl commented Jul 1, 2019

Yeah, I'm going to close this as kinda sneaky, almost hidden control flow.

@shawnl shawnl closed this as completed Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants