Zig: support top level fields

Created on 2 Mar 2019  路  5Comments  路  Source: ziglang/zig

Now that #1047 is done, and every file is implicitly an empty struct, we could allow fields at the top level. This would solve the redundant import problem:

https://github.com/ziglang/zig/blob/96164ce61377b36bcaf0c4087ca9b1ab822b9457/std/index.zig#L3-L10

Syntactically it looks a bit odd, but semantically it makes sense.

accepted contributor friendly proposal

Most helpful comment

I was in the mood for a little bit of bikeshedding, so I refactored my project to use toplevel fields wherever possible. No issues so far on zig 0.6.0. Repository here for the curious: https://github.com/ifreund/river.

All 5 comments

This seems to already work in stage 1:

a: u8,

test "" {
    var s = @This(){ .a = 22 };
    @import("std").testing.expectEqual(u8(22), s.a);
}

When I did the grammar rework I allowed fields at top level in the syntax, and now that files are empty structs, it seems to work it just haven't been tested. To close this, we probably need:

  • Tests
  • std refactor
  • Stage 2 parser update

ah! I got tricked by vim integration with zig fmt :laughing:

Also just to repeat this here:

The "index.zig" convention is deprecated. The new pattern is the one set by

  • std/os/darwin.zig (and corresponding std/os/darwin/ folder)
  • std/event.zig (and corresponding std/event/ folder)

Also, files cannot be generic so the redundant import problem is only solved for none generic structs.

I was in the mood for a little bit of bikeshedding, so I refactored my project to use toplevel fields wherever possible. No issues so far on zig 0.6.0. Repository here for the curious: https://github.com/ifreund/river.

Was this page helpful?
0 / 5 - 0 ratings