mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
Clarify the methods syntax sugar & a bit more
I think it's a bit clearer to show exactly what the syntax sugar of methods is, because that's all it is. Every function in Zig is in a struct (files are structs after all) and methods just simplify their use. I also thought we might use the explicit saturating subtraction as that is why the feature is in Zig.
This commit is contained in:
parent
f83fe12dce
commit
18f69f5634
1 changed files with 24 additions and 19 deletions
|
@ -2,9 +2,9 @@
|
||||||
// Help! Evil alien creatures have hidden eggs all over the Earth
|
// Help! Evil alien creatures have hidden eggs all over the Earth
|
||||||
// and they're starting to hatch!
|
// and they're starting to hatch!
|
||||||
//
|
//
|
||||||
// Before you jump into battle, you'll need to know four things:
|
// Before you jump into battle, you'll need to know three things:
|
||||||
//
|
//
|
||||||
// 1. You can attach functions to structs:
|
// 1. You can attach functions to structs (and other "type definitions"):
|
||||||
//
|
//
|
||||||
// const Foo = struct{
|
// const Foo = struct{
|
||||||
// pub fn hello() void {
|
// pub fn hello() void {
|
||||||
|
@ -12,31 +12,34 @@
|
||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
//
|
//
|
||||||
// 2. A function that is a member of a struct is a "method" and is
|
// 2. A function that is a member of a struct is "namespaced" within
|
||||||
// called with the "dot syntax" like so:
|
// that struct and is called by specifying the "namespace" and then
|
||||||
|
// using the "dot syntax":
|
||||||
//
|
//
|
||||||
// Foo.hello();
|
// Foo.hello();
|
||||||
//
|
//
|
||||||
// 3. The NEAT feature of methods is the special parameter named
|
// 3. The NEAT feature of these functions is that if they take either
|
||||||
// "self" that takes an instance of that type of struct:
|
// an instance of the struct or a pointer to an instance of the struct
|
||||||
|
// then they have some syntax sugar:
|
||||||
//
|
//
|
||||||
// const Bar = struct{
|
// const Bar = struct{
|
||||||
// number: u32,
|
// pub fn a(self: Bar) void { _ = self; }
|
||||||
//
|
// pub fn b(this: *Bar, other: u8) void { _ = this; _ = other; }
|
||||||
// pub fn printMe(self: Bar) void {
|
// pub fn c(bar: *const Bar) void { _ = bar; }
|
||||||
// std.debug.print("{}\n", .{self.number});
|
|
||||||
// }
|
|
||||||
// };
|
// };
|
||||||
//
|
//
|
||||||
// (Actually, you can name the first parameter anything, but
|
// var bar = Bar{};
|
||||||
// please follow convention and use "self".)
|
// bar.a() // is equivalent to Bar.a(bar)
|
||||||
|
// bar.b(3) // is equivalent to Bar.b(&bar, 3)
|
||||||
|
// bar.c() // is equivalent to Bar.c(&bar)
|
||||||
//
|
//
|
||||||
// 4. Now when you call the method on an INSTANCE of that struct
|
// Notice that the name of the parameter doesn't matter. Some use
|
||||||
// with the "dot syntax", the instance will be automatically
|
// self, others use a lowercase version of the type name, but feel
|
||||||
// passed as the "self" parameter:
|
// free to use whatever is most appropriate.
|
||||||
//
|
//
|
||||||
// var my_bar = Bar{ .number = 2000 };
|
// Effectively, the method syntax sugar just does this transformation:
|
||||||
// my_bar.printMe(); // prints "2000"
|
// thing.function(args);
|
||||||
|
// @TypeOf(thing).function(thing, args);
|
||||||
//
|
//
|
||||||
// Okay, you're armed.
|
// Okay, you're armed.
|
||||||
//
|
//
|
||||||
|
@ -63,7 +66,9 @@ 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 -= if (self.damage >= alien.health) alien.health else self.damage;
|
alien.health -|= self.damage; // Saturating inplace substraction
|
||||||
|
// It subtracts but doesn't go below the
|
||||||
|
// lowest value for our type (in this case 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue