Skip to content

struct passed by value OK if function is not called #555

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
PavelVozenilek opened this issue Oct 20, 2017 · 1 comment
Closed

struct passed by value OK if function is not called #555

PavelVozenilek opened this issue Oct 20, 2017 · 1 comment
Labels
question No questions on the issue tracker, please.

Comments

@PavelVozenilek
Copy link

Copied on request from #548 (comment).

Docs states:

In Zig, structs, unions, and enums with payloads cannot be passed by value to a function.

However this compiles:

const foo = struct 
{
  x : i32,
  y : i32,
};

fn bar(f : foo) {}

pub fn main() -> %void {

  var f : foo = undefined;
  //bar(f);  // <<== it stops to compile when I uncomment this
}

This feels like a bug, the offending function has to be called for error to show.

@andrewrk andrewrk changed the title 0.1.1 struct passed by value OK if function is not called struct passed by value OK if function is not called Oct 20, 2017
@andrewrk andrewrk added the question No questions on the issue tracker, please. label Oct 20, 2017
@andrewrk
Copy link
Member

Zig has lazy analysis of top level declarations.

It's so you can do something like this:

if (is_windows) { // assume `is_windows` is compile-time known
    functionThatOnlyWorksOnWindows();
}

functionThatOnlyWorksOnWindows is only analyzed if it is called.

This also helps us compile faster; we don't waste effort on dead code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

2 participants