-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
this rides off the back of #6617 but provides a new solution that also makes Zig simpler while allowing more freedom and explicitness.
many Zig projects, including Zig itself, take advantage of the side effect that files are processed as structs to reduce the amount of characters needed to be able to do something along the lines of const Compilation = @import("Compilation.zig");. However, this shortcut is inconsistent, requires this knowledge of the compiler internals, and only works with structs.
But the idea of a file representing a single unit of code is very common and in that very same file, we have const Value = @import("value.zig").Value; and const Type = @import("type.zig").Type; because the main value of those files is an extern union and not a struct so the bare import trick doesn't work.
The syntax would roughly look like pub default <const|var> <expression>;. Other pubs in the namespace would be compile errors.
Ideally, this proposal would also enforce #6617 getting accepted, but offers a solution that allows all the following to work:
const Compilation = @import("Compilation.zig");const Value = @import("Value.zig");const Type = @import("Type.zig");
since you would be able to default export any type, whether it be a struct, union, enum, fn, etc.
Compilers don't need files to make a program, but they're critical for the human organization of code.
So parsing this file would look like reading the top level declarations, if a default pub is found, then the type of the file is the value of the expression, otherwise the file is an opaque type with the discovered declarations.