.zon and init with .decl_literal

It looks like it is not possible to initialize a struct or tuple field with a .decl_literal in zon files.

How difficult would it be to implement this?

I have just searched through the Zig sources and found that AstGen and ZonGen handle .enum_literal differently. But can we achieve the same behavior as we have in regular .zig files?

It should be possible to implement this in the parser, but the new logic wouldn’t be in ZonGen, since that doesn’t have access to the type you are parsing it into (an enum literal and a decl literal appear the same if you don’t know what the result type is).

From glancing at std.zon.parse, it looks like it might not be too difficult to support this. The bigger question though is whether this is considered a proper use of ZON. I don’t know much about the exact philosophy behind ZON beyond the fact that it’s meant to parse into zig types, and I don’t think there’s any formal documentation for it yet. If the intention is for ZON to exactly represent the right hand side of an assignment then maybe decl literals will be a supported feature, but my gut tells me that they won’t be supported even if you find a good way to implement it.

2 Likes

field and decl names cant conflict so it can just be resolved later when the type is known

1 Like