ziglings/exercises/092_interfaces.zig

128 lines
3.9 KiB
Zig
Raw Permalink Normal View History

2023-02-11 05:12:47 -05:00
//
2023-02-15 17:45:10 -05:00
// Remember our ant and bee simulator constructed with unions
// back in exercises 55 and 56? There, we demonstrated that
// unions allow us to treat different data types in a uniform
// manner.
2023-02-11 05:12:47 -05:00
//
2023-02-15 17:45:10 -05:00
// One neat feature was using tagged unions to create a single
// function to print a status for ants *or* bees by switching:
//
// switch (insect) {
// .still_alive => ... // (print ant stuff)
// .flowers_visited => ... // (print bee stuff)
// }
//
// Well, that simulation was running just fine until a new insect
// arrived in the virtual garden, a grasshopper!
//
// Doctor Zoraptera started to add grasshopper code to the
// program, but then she backed away from her keyboard with an
// angry hissing sound. She had realized that having code for
// each insect in one place and code to print each insect in
// another place was going to become unpleasant to maintain when
// the simulation expanded to hundreds of different insects.
//
// Thankfully, Zig has another comptime feature we can use
2023-06-17 03:34:37 -04:00
// to get out of this dilemma called the 'inline else'.
2023-02-15 17:45:10 -05:00
//
// We can replace this redundant code:
//
// switch (thing) {
// .a => |a| special(a),
// .b => |b| normal(b),
// .c => |c| normal(c),
// .d => |d| normal(d),
// .e => |e| normal(e),
// ...
// }
//
// With:
//
// switch (thing) {
// .a => |a| special(a),
2023-05-05 06:11:20 -04:00
// inline else => |t| normal(t),
2023-02-15 17:45:10 -05:00
// }
//
// We can have special handling of some cases and then Zig
// handles the rest of the matches for us.
//
// With this feature, you decide to make an Insect union with a
// single uniform 'print()' function. All of the insects can
// then be responsible for printing themselves. And Doctor
// Zoraptera can calm down and stop gnawing on the furniture.
2023-02-11 05:12:47 -05:00
//
const std = @import("std");
const Ant = struct {
still_alive: bool,
pub fn print(self: Ant) void {
2023-02-15 17:45:10 -05:00
std.debug.print("Ant is {s}.\n", .{if (self.still_alive) "alive" else "dead"});
2023-02-11 05:12:47 -05:00
}
};
const Bee = struct {
flowers_visited: u16,
pub fn print(self: Bee) void {
std.debug.print("Bee visited {} flowers.\n", .{self.flowers_visited});
}
};
2023-02-15 17:45:10 -05:00
// Here's the new grasshopper. Notice how we've also added print
// methods to each insect.
2023-02-11 05:12:47 -05:00
const Grasshopper = struct {
distance_hopped: u16,
pub fn print(self: Grasshopper) void {
2023-02-15 17:45:10 -05:00
std.debug.print("Grasshopper hopped {} meters.\n", .{self.distance_hopped});
2023-02-11 05:12:47 -05:00
}
};
const Insect = union(enum) {
ant: Ant,
bee: Bee,
grasshopper: Grasshopper,
2023-02-15 17:45:10 -05:00
// Thanks to 'inline else', we can think of this print() as
// being an interface method. Any member of this union with
2023-05-07 04:46:07 -04:00
// a print() method can be treated uniformly by outside
2023-02-15 17:45:10 -05:00
// code without needing to know any other details. Cool!
2023-02-11 05:12:47 -05:00
pub fn print(self: Insect) void {
switch (self) {
inline else => |case| return case.print(),
}
}
};
pub fn main() !void {
const my_insects = [_]Insect{
2023-02-15 17:45:10 -05:00
Insect{ .ant = Ant{ .still_alive = true } },
Insect{ .bee = Bee{ .flowers_visited = 17 } },
Insect{ .grasshopper = Grasshopper{ .distance_hopped = 32 } },
2023-02-15 17:45:10 -05:00
};
2023-02-11 05:12:47 -05:00
2023-02-15 17:45:10 -05:00
std.debug.print("Daily Insect Report:\n", .{});
for (my_insects) |insect| {
// Almost done! We want to print() each insect with a
// single method call here.
2024-06-19 12:19:46 -04:00
insect.print();
2023-02-11 05:12:47 -05:00
}
}
2023-02-15 17:45:10 -05:00
// Our print() method in the Insect union above demonstrates
// something very similar to the object-oriented concept of an
// abstract data type. That is, the Insect type doesn't contain
// the underlying data, and the print() function doesn't
// actually do the printing.
//
// The point of an interface is to support generic programming:
// the ability to treat different things as if they were the
// same to cut down on clutter and conceptual complexity.
//
// The Daily Insect Report doesn't need to worry about *which*
// insects are in the report - they all print the same way via
// the interface!
//
// Doctor Zoraptera loves it.