Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add self method parameter name to style guide #499

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading