diff options
| author | Arya-Elfren <[email protected]> | 2023-04-28 11:12:42 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-28 11:12:42 +0100 |
| commit | 3612c67f04e0d902a12c3f71ed52b1de8422804e (patch) | |
| tree | 2b5bd31ad13e4c363d794d23e2a311cd8660463c | |
| parent | 18f69f5634c7469042dc601e4c5609af9e0f382c (diff) | |
| download | ziglings-3612c67f04e0d902a12c3f71ed52b1de8422804e.tar.xz ziglings-3612c67f04e0d902a12c3f71ed52b1de8422804e.zip | |
Simplify methods explanation in 047
| -rw-r--r-- | exercises/047_methods.zig | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index 7211caa..6b2dbef 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -18,14 +18,14 @@ // // Foo.hello(); // -// 3. The NEAT feature of these functions is that if they take either -// an instance of the struct or a pointer to an instance of the struct -// then they have some syntax sugar: +// 3. The NEAT feature of these functions is that if their first argument +// is an instance of the struct (or a pointer to one) then we can use +// the instance as the namespace instead of the type: // // const Bar = struct{ -// pub fn a(self: Bar) void { _ = self; } -// pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; } -// pub fn c(bar: *const Bar) void { _ = bar; } +// pub fn a(self: Bar) void {} +// pub fn b(this: *Bar, other: u8) void {} +// pub fn c(bar: *const Bar) void {} // }; // // var bar = Bar{}; @@ -37,10 +37,6 @@ // self, others use a lowercase version of the type name, but feel // free to use whatever is most appropriate. // -// Effectively, the method syntax sugar just does this transformation: -// thing.function(args); -// @TypeOf(thing).function(thing, args); -// // Okay, you're armed. // // Now, please zap the alien structs until they're all gone or @@ -66,9 +62,7 @@ const HeatRay = struct { // We love this method: pub fn zap(self: HeatRay, alien: *Alien) void { - alien.health -|= self.damage; // Saturating inplace substraction - // It subtracts but doesn't go below the - // lowest value for our type (in this case 0) + alien.health -= if (self.damage >= alien.health) alien.health else self.damage; } }; |
