3 ms·
Yeah my choice of words wasn't great. It seams as if the zig std acts as a macro rather than a wrapper. That could just be the aggressive optimization though
by mcountryman 6y ago
Yeah my choice of words wasn't great. It seams as if the zig std acts as a macro rather than a wrapper. That could just be the aggressive optimization though
- acqq 6y agoWhat name would you use after seeing the implementation of the functions called? std/debug.zig: pub fn print(comptime fmt: []const u8, args: anytype) void { const held = stderr_mutex.acquire(); defer held.release(); const stderr = io.getStdErr().writer(); nosuspend stderr.print(fmt, args) catch return; } io/writer.zig: pub fn print(self: Self, comptime format: []const u8, args: anytype) Error!void { return std.fmt.format(self, format, args); } std/fmt.zig: pub fn format( writer: anytype, comptime fmt: []const u8, args: anytype, ) !void { ... It's all based on the language features and the actual implementations of zig's standard library code, and at least I don't see anything "wrapped" there, and nothing what C++, C or asm programmers understand as "a macro".
- mcountryman 6y agoVery well optimized inlined functions? I didn't actually check the zig std source. I was referring to what I was seeing in radare2 :)
- acqq 6y agoIn zig there's a concept of "comptime" -- note that fmt parameter is marked as such. It's more than just inlining. https://ziglang.org/documentation/master/#comptime https://ziglang.org/documentation/master/#comptime See an example calling firstNPrimes. "When we compile this program, Zig generates the constants with the answer pre-computed." "Note that we did not have to do anything special with the syntax of these functions. For example, we could call the sum function as is with a slice of numbers whose length and values were only known at run-time." Then see "Case Study: printf in Zig"
- mcountryman 6y agoI'm a little short on time so I didn't read through all of it but, comptime generics sounds similar to how rust compiles generics and comptime parameters are similar to const fns although zig appears to have more control over what is deamed "compile time".