From c5afb1eee4e508640f3280a1d3862a01188186e3 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Wed, 15 Jan 2025 17:57:24 -0300 Subject: [PATCH] docs: add `self` method parameter name to style guide --- docs/CONTRIBUTING.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 9f7dae3eb..272622617 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -31,6 +31,23 @@ There are two main guidelines to keep in mind when naming interfaces and interfa - As an example, a generic implementation of `std.Random` which is a pseudo-random number generator should be named `prng` (ie this is relevant when making appropriate use of `std.Random.DefaultPrng`). - As another example, an instance of `std.heap.GeneralPurposeAllocator(config)` should be called `gpa_state`, `std.heap.ArenaAllocator` `arena_state`, and so on. +#### Method Parameters +The first parameter of a method should be named `self`. The type should be the name of the struct. +For example: + +```zig +const MyStruct = struct { + state: u8, + + fn write(self: *MyStruct, new_state: u8) void { + self.state = new_state; + } +}; +``` + +If the type name is not available (for example in anonymous structs), define `const Self = @This()` +and use that as the type. + ### Files as Structs We prohibit usage of files as instantiable struct types in the codebase.