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 top level fields #2022

Closed
andrewrk opened this issue Mar 1, 2019 · 5 comments
Closed

support top level fields #2022

andrewrk opened this issue Mar 1, 2019 · 5 comments
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Mar 1, 2019

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:

zig/std/index.zig

Lines 3 to 10 in 96164ce

pub const BufMap = @import("buf_map.zig").BufMap;
pub const BufSet = @import("buf_set.zig").BufSet;
pub const Buffer = @import("buffer.zig").Buffer;
pub const BufferOutStream = @import("buffer.zig").BufferOutStream;
pub const HashMap = @import("hash_map.zig").HashMap;
pub const LinkedList = @import("linked_list.zig").LinkedList;
pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList;
pub const SegmentedList = @import("segmented_list.zig").SegmentedList;

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

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Mar 1, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Mar 1, 2019
@Hejsil
Copy link
Sponsor Contributor

Hejsil commented Mar 1, 2019

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

@andrewrk
Copy link
Member Author

andrewrk commented Mar 1, 2019

ah! I got tricked by vim integration with zig fmt 😆

@andrewrk andrewrk changed the title allow top level fields support top level fields Mar 1, 2019
@andrewrk
Copy link
Member Author

andrewrk commented Mar 1, 2019

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)

@Hejsil
Copy link
Sponsor Contributor

Hejsil commented Mar 4, 2019

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

@andrewrk andrewrk added the accepted This proposal is planned. label Apr 30, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 30, 2019
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Jan 8, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 8, 2020
@ifreund
Copy link
Member

ifreund commented May 2, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants