Simplify methods explanation in 047

pull/2/head
Arya-Elfren 1 year ago committed by GitHub
parent 18f69f5634
commit 3612c67f04

@ -18,14 +18,14 @@
// //
// Foo.hello(); // Foo.hello();
// //
// 3. The NEAT feature of these functions is that if they take either // 3. The NEAT feature of these functions is that if their first argument
// an instance of the struct or a pointer to an instance of the struct // is an instance of the struct (or a pointer to one) then we can use
// then they have some syntax sugar: // the instance as the namespace instead of the type:
// //
// const Bar = struct{ // const Bar = struct{
// pub fn a(self: Bar) void { _ = self; } // pub fn a(self: Bar) void {}
// pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; } // pub fn b(this: *Bar, other: u8) void {}
// pub fn c(bar: *const Bar) void { _ = bar; } // pub fn c(bar: *const Bar) void {}
// }; // };
// //
// var bar = Bar{}; // var bar = Bar{};
@ -37,10 +37,6 @@
// self, others use a lowercase version of the type name, but feel // self, others use a lowercase version of the type name, but feel
// free to use whatever is most appropriate. // 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. // Okay, you're armed.
// //
// Now, please zap the alien structs until they're all gone or // Now, please zap the alien structs until they're all gone or
@ -66,9 +62,7 @@ const HeatRay = struct {
// We love this method: // We love this method:
pub fn zap(self: HeatRay, alien: *Alien) void { pub fn zap(self: HeatRay, alien: *Alien) void {
alien.health -|= self.damage; // Saturating inplace substraction alien.health -= if (self.damage >= alien.health) alien.health else self.damage;
// It subtracts but doesn't go below the
// lowest value for our type (in this case 0)
} }
}; };

Loading…
Cancel
Save