I want to know when a method needs to take the outer type as first parameter? Take the common Iterator as example: A next() method of type Iterator usually takes the type it lives in as first parameter and you don’t have to fill in as argument at call site; an init() method of type Iterator doesn’t care about that. E.g., next(it: *Iterator) ?u8 and init(vector: []const u8) Iteratior
I found there are two patterns: 1) doThing(self: @This()) 2) doAnotherThing(self: Self) with const Self = @This(); As for return type, maybe Self, maybe MyType though self: Self or self: @This() used.
Some people just use self. I’ve seen it advocated, and I agree, to name it something meaningful instead. For example if i’m in a function pub fn explode(self: Player) self isn’t very descriptive and I find it more useful to provide the context I'm acting on a player, than I'm acting on whatever the current self object is
So I’d prefer pub fn explode(player: Player) or something that provides more information
personally I’ve come back from the self: Self pattern.
First it’s uninformative, if you jump in the middle of a big file and you just see self: Self it’s not helpful. Am I looking at the manager or unmanaged version of the hashmap ?
Second it doesn’t play well with nesting, if you nest your struct the nested one can’t use Self again
I tend to spell out types using their fully qualified name w.r.t to the outermost scope, especially when the type name is something rather uninformative on its own, and only makes sense when coupled to the owning type, eg
I reserve the use of const Self = @This() for templated types, because due to Zig’s ability to nest types and methods, seeing a wild Self or @This() is absolutely unhelpful, and I don’t like to strain my eyes counting levels of indentations. Generic types are the only exception, because I often give the type function the name I want to give the type, and you can’t shadow names.
Incidentally, as this is a problem I rarely encounter in other languages, this has forced to think a lot more about the names I give types.