From 3612c67f04e0d902a12c3f71ed52b1de8422804e Mon Sep 17 00:00:00 2001 From: Arya-Elfren <109028294+Arya-Elfren@users.noreply.github.com> Date: Fri, 28 Apr 2023 11:12:42 +0100 Subject: [PATCH] Simplify methods explanation in 047 --- exercises/047_methods.zig | 20 +++++++------------- 1 file 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; } };