#How do you write files?

1 messages · Page 1 of 1 (latest)

ivory sleet
#

Coming from other languages it felt natural to write a file like this:
------ Duck.zig
pub const Duck = struct {
const can_migrate: bool = false;
quack_count: u32,
pub fn quack(self: *Duck) { ... }
}

I realized I didn't have to do the struct at all since @import returns a struct anyway:
------ Duck.zig
const Self = @This();
const can_migrate: bool = false;
quack_count: u32,
pub fn quack(self: *Self) { ... }

I'd be intrested knowing what you guys think about this and what you usually do? 🙂

severe viper
#

Both are useful. Probably wouldn't want to create a zig file for every little struct, but for larger structs that form a signifficant interface to something, it makes sense to dedicate a whole zig module for it.

If you read the zig std source, you can see that that's exactly what they do - have a fusion of both styles of struct declarations

ivory sleet
#

Thanks @severe viper

#

I was also wondering if there's a overhead to having a struct within a struct. "the one imported and the one in the file"

severe viper
#

Not really, that's a legitemate use case and designed that way. In fact zig's namespaces are implemented as comptime known fields of nested structs.
So the whole const std = @import("std"); is a deeply nested struct

ivory sleet
#

Alright thanks. So I should just not worry about it