Basic Awareness in Addition to Deep Understanding
Related
More from openmymind.net
In an older post, we explored Zig's @ptrCast and then looked at a concrete usage in the shape of std.heap.MemoryPool. @ptrCast As a brief recap, when we use @ptrCast, we're telling the compiler to treat a pointer as a given type. For example, this code runs fine: const std = @import("std"); pub fn main() !void { var user = User{.power = 9001,...
First, the code: std.mem.sort([]const u8, values, {}, stringLessThan); fn stringLessThan(_: void, lhs: []const u8, rhs: []const u8) bool { return std.mem.order(u8, lhs, rhs) == .lt; } std.mem.sort takes 4 arguments: the type of value we're sorting, the list of values to sort, an arbitrary context, and a function. The last argument, the...
If I asked you to respond to an HTTP request with a JSON serialize list of products, somewhere in your code, you'd probably have (or whatever the equivalent is in your stack): body, err := json.Marshal(products) There's an alternative to this approach that I'm rather fond of: gluing pre-serialized JSON pieces together: if (len(productJSON)) == 0...
A recently merged pull request removed anonymous struct from Zig. I was surprised by this change - it seemed like a big deal. But it turns out that I didn't understand what an anonymous struct were, and this change isn't quite as big as I thought. Consider this code: const std = @import("std"); pub fn main() !void { const user = .{.id = 2,...
In the following code, we create a Post structure with a skeleton format method. Despite being pretty comfortable with Zig, I could stare at this code for hours and not realize that it has two issues. pub fn main() !void { } const Post = struct { raw: []const u8, pub fn format(self: Post, format: Format) void { _ = self; _...