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

ability to @import .zon files #14531

Open
Tracked by #8 ...
andrewrk opened this issue Feb 3, 2023 · 8 comments
Open
Tracked by #8 ...

ability to @import .zon files #14531

andrewrk opened this issue Feb 3, 2023 · 8 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Feb 3, 2023

Extracted from #14523.

Given a foo.zon file:

.{
    .bar = "hello",
}

it should be possible to @import it as a comptime-known value:

const data = @import("foo.zon");
comptime {
    assert(std.mem.eql(u8, data.bar, "hello"));
}

One motivating use case in particular is, inside build.zig, one should be able to @import("build.zig.zon") and access those values.

Note that the top level expression of a zon file is not necessarily an anonymous struct literal. It can be:

  • string literal
  • boolean literal
  • integer literal
  • null literal
  • undefined literal
  • anonymous struct literal
  • tuple literal
  • anonymous enum literal
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Feb 3, 2023
@andrewrk andrewrk added this to the 0.12.0 milestone Feb 3, 2023
@f3fora
Copy link

f3fora commented Feb 4, 2023

"Zig source files are implicitly structs" ziglang.org. Why is this philosophy not followed in the zon ones too?

Given the following snippet with an import for foo.zon

const data = @import("foo.zon");
comptime {
    assert(std.mem.eql(u8, data.bar, "hello"));
}

my expectation for a foo.zon file is

.bar = "hello",

and not

.{
    .bar = "hello",
}

@SidharthRG
Copy link

"Zig source files are implicitly structs" ziglang.org. Why is this philosophy not followed in the zon ones too?

This was actually my first thought as well. But then,

.bar = "hello",

likely wouldn't work. It'd have to be written as:

const bar = "hello";

But now we're just writing code in the ZON file, which defeats the purpose of an object notation.


As presented so far, it seems like the ZON format is intended to be parsed at compile time for resolving package dependencies. Is runtime parsing in application code also going to be allowed? That would allow ZON files to be used to specify app configs (replace a settings.json, say, with settings.zon).

@ducktype
Copy link

Hello, i'm also interested in runtime parsing of zon files!

@InKryption
Copy link
Contributor

This seems to be the only issue I could find in which there is any description of what is allowed in zon files, so I'll ask these questions here:

Zon files are allowed to be, or to contain, zig literals: what about expressions that evaluate to the equivalent of these literals? E.g. is "foo" ++ "bar" allowed as a zon object? Or how about 4 * 1? Or even simpler, is ('a') allowed?

(Similar to the first question) Should &.{} be allowed to describe an empty list, or should .{} be used to describe both empty/defaulted structs and empty lists?.

Do multi-line string literals fall under "string literal"?

In the case of a value being undefined in a zon file, does that mean the imported value will also be treated as regular undefined? Is this safe? How do we handle an undefined value coming from an imported zon file, and how should we detect/know to initialize such a value? It seems sort of like a footgun to me.

@iacore
Copy link
Contributor

iacore commented Dec 27, 2023

Right now, const val: T = <zon here>; is just as good. It also type checks for you.

@jcs090218
Copy link

The runtime zon file benefits the most, so I second it. :D

Another question is, can a zon file be empty? If yes, what would it be? An undefined? 🤔

@Arnau478
Copy link
Contributor

Another question is, can a zon file be empty? If yes, what would it be? An undefined? 🤔

Looking at the parsing code, it doesn't seem to allow that, as Ast.parse ends up parsing an expression, and encountering EOF during expression parsing is an error. Plus, I don't see any benefits of having empty zon files. As I said, they're expressions, so if you want it to be undefined you can just create a zon file with undefined on it.

@linusg
Copy link
Contributor

linusg commented Jul 6, 2024

The "retrieve version from build.zig.zon" use case might be addressed by #20510, which exposes it through b.manifest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

9 participants