Added ex 48, additional comment on 46
parent
11ec69b92c
commit
5f7e9389d5
@ -0,0 +1,68 @@
|
||||
//
|
||||
// Now that we've seen how methods work, let's see if we can help
|
||||
// our elephants out a bit more with some Elephant methods.
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
const Elephant = struct {
|
||||
letter: u8,
|
||||
tail: ?*Elephant = null,
|
||||
visited: bool = false,
|
||||
|
||||
// New Elephant methods!
|
||||
pub fn getTail(self: *Elephant) *Elephant {
|
||||
return self.tail.?; // Remember, this is means "orelse unreachable"
|
||||
}
|
||||
|
||||
pub fn hasTail(self: *Elephant) bool {
|
||||
return (self.tail != null);
|
||||
}
|
||||
|
||||
pub fn visit(self: *Elephant) void {
|
||||
self.visited = true;
|
||||
}
|
||||
|
||||
pub fn print(self: *Elephant) void {
|
||||
// Prints elephant letter and (V)isited or (U)nvisited.
|
||||
var v: u8 = if (self.visited) 'V' else 'U';
|
||||
std.debug.print("Elephant {u} ({u}). ", .{ self.letter, v});
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() void {
|
||||
var elephantA = Elephant{ .letter = 'A' };
|
||||
var elephantB = Elephant{ .letter = 'B' };
|
||||
var elephantC = Elephant{ .letter = 'C' };
|
||||
|
||||
// Link the elephants so that each tail "points" to the next.
|
||||
elephantA.tail = &elephantB;
|
||||
elephantB.tail = &elephantC;
|
||||
|
||||
visitElephants(&elephantA);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
// This function visits all elephants once, starting with the
|
||||
// first elephant and following the tails to the next elephant.
|
||||
fn visitElephants(first_elephant: *Elephant) void {
|
||||
var e = first_elephant;
|
||||
|
||||
while (true) {
|
||||
e.print();
|
||||
e.visit();
|
||||
|
||||
// Get the next elephant or stop.
|
||||
if (e.hasTail()) {
|
||||
e = e.???; // Which method do we want here?
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bonus: Zig's enums can also have methods! Can you find
|
||||
// one in the wild? If you can, mention it along with your
|
||||
// name or alias in a comment below this one and make a
|
||||
// pull request on GitHub for a piece of eternal Ziglings
|
||||
// glory. The first five (5) PRs will be accepted!
|
@ -1,8 +1,21 @@
|
||||
12c12
|
||||
< tail: *Elephant = null, // <---- make this optional!
|
||||
8,19d7
|
||||
< // We also introduce the handy ".?" shortcut:
|
||||
< //
|
||||
< // const foo = bar.?;
|
||||
< //
|
||||
< // is the same as
|
||||
< //
|
||||
< // const foo = bar orelse unreachable;
|
||||
< //
|
||||
< // See if you can find where we use this shortcut below.
|
||||
< //
|
||||
< // Now let's make those elephant tails optional!
|
||||
< //
|
||||
24c12
|
||||
< tail: *Elephant = null, // Hmm... tail needs something...
|
||||
---
|
||||
> tail: ?*Elephant = null, // <---- make this optional!
|
||||
42c42
|
||||
54c42
|
||||
< if (e.tail == null) ???;
|
||||
---
|
||||
> if (e.tail == null) break;
|
||||
|
@ -0,0 +1,4 @@
|
||||
57c57
|
||||
< e = e.???; // Which method do we want here?
|
||||
---
|
||||
> e = e.getTail(); // Which method do we want here?
|
Loading…
Reference in New Issue