Merge pull request #265 from Arya-Elfren/methods-clarification

Clarify the methods syntax sugar & a bit more
pull/2/head
Dave Gauer 1 year ago committed by GitHub
commit 7a44e4d342

@ -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,30 @@
// } // }
// }; // };
// //
// 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 their first argument
// "self" that takes an instance of that type of struct: // 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{ // const Bar = struct{
// number: u32, // pub fn a(self: Bar) void {}
// // pub fn b(this: *Bar, other: u8) void {}
// pub fn printMe(self: Bar) void { // pub fn c(bar: *const Bar) void {}
// 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)
// 4. Now when you call the method on an INSTANCE of that struct // bar.c() // is equivalent to Bar.c(&bar)
// with the "dot syntax", the instance will be automatically
// passed as the "self" parameter:
// //
// var my_bar = Bar{ .number = 2000 }; // Notice that the name of the parameter doesn't matter. Some use
// my_bar.printMe(); // prints "2000" // self, others use a lowercase version of the type name, but feel
// free to use whatever is most appropriate.
// //
// Okay, you're armed. // Okay, you're armed.
// //

Loading…
Cancel
Save