Skip to content

initialization of empty structs #554

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

initialization of empty structs #554

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).

Zig requires initialization, even for empty structs:

const foo = struct {};

pub fn main() -> %void {

  var f : foo;
}

results in :

 
O:\zig0.1.1>zig build-exe b.zig
O:\zig0.1.1\b.zig:11:3: error: variables must be initialized
  var f : foo;
  ^

While empty struct does not make much of sense, asking for its initialization is IMO error.

@andrewrk andrewrk added the question No questions on the issue tracker, please. label Oct 20, 2017
@andrewrk andrewrk changed the title 0.1.1 initialization of empty structs initialization of empty structs Oct 20, 2017
@andrewrk
Copy link
Member

You have 2 options for initializing a struct, whether it has 0 or more fields:

Option 1, recommended, list all the fields in the initializer. With a struct with no fields, there are 0 fields in the initializer. This option is recommended because if you add a field later, you'll get a compiler error here for not initializing all fields.

var f = foo {}; 

Option 2, leave all fields undefined:

var f: foo = undefined;

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