4 ms·
> std.debug.print("foo param: {}\n", .{x}); Why is this ".{x}" and not just "x"? This syntax is used tens of times in the following code snippets but never e
by hota_mazi 6y ago
> std.debug.print("foo param: {}\n", .{x});
Why is this ".{x}" and not just "x"?
This syntax is used tens of times in the following code snippets but never explained.
- dnautics 6y agoThanks for the catch! It's an anonymous array. Later down there is a place where there are two sets of brackets in the print string, and the so the anonymous array passed to it has to have two items (the string is analyzed and the correct size of the array is compile-time checked, no stack drilling here).
- hota_mazi 6y agoDoes this mean that "print" only takes two arguments, and the second argument has to be an array? This is the kind of information that your tutorial needs to contain, because it's radically different from C and other languages, and this is exactly the kind of information that readers will be looking for: "How different is Zig from C and why should I care?".
- dnautics 6y agoYep! The document has now been updated. I might have been blind to it because my daily driver is Elixir, and it does "basically the same thing" with keyword arguments.
- hota_mazi 6y agoI totally understand why being immersed in something might make you lose perspective, but keeping this perspective is what differentiates a decent language designer from a great one. Does this mean that zig doesn't support varargs? If not, why not? Again, another question that a lot of people coming to your page will be asking themselves, please don't take that as a criticism, just candid feedback to help you make your article better.
- dnautics 6y agoI'm not the creator of zig! Thank you for your advice. Zig does not support varargs, except when calling C functions.
- edflsafoiewq 6y agoZig does not have varargs. They were removed in favor of anonymous structs/tuples. See https://github.com/ziglang/zig/issues/208#issuecomment-393777148 https://github.com/ziglang/zig/issues/208#issuecomment-39377....
- aidenn0 6y agoIsn't it an anonymous struct, not an anonymous array? (IIRC it can't be an array, since it must support heterogenous types)
- dnautics 6y agoYou're right! It's an anonymous struct with numbered fields. I forgot about that.
- deleted 6y ago[deleted]
- zamadatix 6y agoTo allow for multiple substitutions to be done the same way.
- hota_mazi 6y agoSorry but that doesn't explain anything. Why is just "x" not allowed? What does ".{x}" actually mean?
- flohofwoe 6y ago.{ x } or .{ x, y, z } etc... are adhoc-constructed anonymous struct-values which can be inspected at compile time by the standard library's string formatting code. It all makes a lot more sense after understanding Zig's comptime features: https://ziglang.org/documentation/master/#comptime https://ziglang.org/documentation/master/#comptime The end of the comptime section also explains how string formatting works that way.