Open przemoc opened 1 year ago
the sections above have the following from structs.zig
// Declare a struct.
const Point = struct {
x: f32,
y: f32,
};
// Declare an instance of a struct.
const p = Point {
.x = 0.12,
.y = 0.34,
};
https://ziglang.org/documentation/master/#Anonymous-Struct-Literals then goes on to explain exactly what is happening with .{
Zig allows omitting the struct type of a literal. When the result is coerced, the struct literal will directly instantiate the result location, with no copy:
The struct type can be inferred. Here the result location does not include a type, and so Zig infers the type:
Anonymous structs can be created without specifying field names, and are referred to as "tuples".
You mostly quoted what I already quoted (+ unrelated stuff about field names). There is no spelling out .{
, i.e. dot followed by curly-brace, and no pointing out it is notation used for anonymous struct literal. For those already in the know, that may seem obvious, but for newcomers trying to follow documenation it is not.
I think Zig Language Reference could be improved in this regard and that's why I created this issue.
Boosting this. I've programmed in something like 30 programming languages and am very interested in Zig and I went through a variety of materials and had to try multiple search incantations before landing on this issue which is the only thing I've found so far that attempts to explain this dot-brace notation. Maybe there are more things and I'm looking in the wrong place but it seems to me as though this could use additional/sooner clarification even if not in the very first hello world example.
I second this. I am learning Zig and wanted to know what the mysterious .{value}
syntax was. This issue is where I got my answer. This should absolutely be mentioned explicitly somewhere especially because it shows up in the literal most basic programs in the language. I understand that this syntax has other more complex uses as well regarding structs, but that is no reason to not give a partial explanation of what it does for beginners.
In Hello World section hello.zig has
.{"world"}
. What is written there is:No explanation of what is the purpose of
.
(dot) before{
(curly-brace). Just like{
+}
are explicitly called out as curly-braces in other context (function's block of statements and expressions are surrounded by them), so should be.{
+}
notation.If we visit Anonymous Struct Literals, there is:
and struct_result.zig, but there is no mentioning of the syntax.
hello_again.zig later has
.{}
, but no comment about this either.Reader can kind of start (after searching for it in more places) guessing that
.
(dot) is some auto thingy that allows deliberate vagueness (less typing) to avoid providing explicit type for struct that can be inferred by the compiler, but guessing is dangerous and clearly explaining what dot followed by curly-braces actually is and mean would be better for newcomers.