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 `if`, `orelse`, `null`, and `.?` for C pointers #1967

andrewrk opened this Issue Feb 15, 2019 · 3 comments


None yet
2 participants
Copy link

andrewrk commented Feb 15, 2019

We could make it easier to transition from auto-generated header file import declarations to more well specified Zig pointers by supporting the optional syntax for C pointers. It would do exactly what you expect.

if (c_pointer) |same_value| {
    // blah
const xyz = c_pointer orelse return;
const abc = c_pointer.?;

would be equivalent to:

if (c_pointer != 0) {
    const same_value = c_pointer;
    // blah
const xyz = if (c_pointer != 0) c_pointer else return;
const abc = if (c_pointer != 0) c_pointer else unreachable;

This is also helpful when you consider that forward declarations are translated as *c_void, and a header file could change to make the forward decl be an actual struct, and then the zig type would change but the C code would expect the type to stay the same. With this proposal it would be less surprising.

An argument for this is that given we want people to avoid C pointers, this syntax would help users transition away from translated code toward more zig-like code.

@andrewrk andrewrk added the proposal label Feb 15, 2019

@andrewrk andrewrk added this to the 0.5.0 milestone Feb 15, 2019


This comment has been minimized.

Copy link

SamTebbs33 commented Feb 17, 2019

Would this be a good issue for a new contributor to take a look at? If so I'd give it a shot with a few pointers (pardon the pun).


This comment has been minimized.

Copy link
Member Author

andrewrk commented Feb 17, 2019

Hi @SamTebbs33 - this might be a bit tricky, especially considering it's not "accepted" yet. I'd hate for you to do some work and then we end up deciding not to do this proposal.

Have you seen the "contributor friendly" issue label?


This comment has been minimized.

Copy link

SamTebbs33 commented Feb 18, 2019

That makes sense. I have seen that label so I'll look through some of those issues to see if I find something intriguing.

@andrewrk andrewrk changed the title support `if`, `orelse`, and `.?` for C pointers support `if`, `orelse`, `null`, and `.?` for C pointers Mar 2, 2019

@andrewrk andrewrk added the accepted label Mar 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.