diff --git a/chapter-1.md b/chapter-1.md index 8edb31e9..3a88fe97 100644 --- a/chapter-1.md +++ b/chapter-1.md @@ -604,7 +604,7 @@ test "set enum ordinal value" { } ``` -Methods can be given to enums. These act as namespaced functions that can be called with dot syntax. +Functions can be given to enums. These are sometimes called "methods", and act as namespaced functions that can be called with dot syntax. As a convenience, when called from an instance of the enum these methods are automatically passed that instance as their first argument. Thus, `Suit.spades.isClubs()` is equivalent to `Suit.isClubs(Suit.spades)`. Additionally, when calling a method from the enum, we can shorten the argument to omit the enum name and pass it with just a leading dot, as in the example below. ```zig const Suit = enum { @@ -691,7 +691,7 @@ test "struct defaults" { Like enums, structs may also contain functions and declarations. -Structs have the unique property that when given a pointer to a struct, one level of dereferencing is done automatically when accessing fields. Notice how in this example, self.x and self.y are accessed in the swap function without needing to dereference the self pointer. +Similar to enums, methods called from a struct instance will be passed that instance, but this time as a *pointer*. Structs also have an additional special property: when accessing fields from a pointer to a struct, one level of dereferencing is done automatically. ```zig const Stuff = struct { @@ -712,6 +712,8 @@ test "automatic dereference" { } ``` +Notice how these work together in the example: the `thing.swap()` function call receives a pointer to `thing` as the `self` parameter, and then uses that pointer to access `self.x` without needing to dereference it. These shorthands are convenient but optional, they are equivalent to `Stuff.swap(&thing)` and `self.*.x` respectively. + # Unions Zig's unions allow you to define types which store one value of many possible typed fields; only one field may be active at one time.