summaryrefslogtreecommitdiffstatshomepage
path: root/exercises/092_interfaces.zig
blob: 5adc9c0163c8e2f768f8068d7588a643761b40fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Remeber excerices 55-57 with tagged unions.
//
// (story/explanation from Dave)
//
const std = @import("std");

const Ant = struct {
    still_alive: bool,

    pub fn print(self: Ant) void {
        std.debug.print("Ant is {s}.\n", .{if (self.still_alive) "alive" else "death"});
    }
};

const Bee = struct {
    flowers_visited: u16,

    pub fn print(self: Bee) void {
        std.debug.print("Bee visited {} flowers.\n", .{self.flowers_visited});
    }
};

const Grasshopper = struct {
    distance_hopped: u16,

    pub fn print(self: Grasshopper) void {
        std.debug.print("Grasshopper hopped {} m.\n", .{self.distance_hopped});
    }
};

const Insect = union(enum) {
    ant: Ant,
    bee: Bee,
    grasshopper: Grasshopper,

    pub fn print(self: Insect) void {
        switch (self) {
            inline else => |case| return case.print(),
        }
    }
};

pub fn main() !void {
    var my_insects = [_]Insect{ Insect{
        .ant = Ant{ .still_alive = true },
    }, Insect{
        .bee = Bee{ .flowers_visited = 17 },
    }, Insect{
        .grasshopper = Grasshopper{ .distance_hopped = 32 },
    } };

    // The daily situation report, what's going on in the garden
    try dailyReport("what is here the right parameter?");
}

// Through the interface we can keep a list of various objects
// (in this case the insects of our garden) and even pass them
// to a function without having to know the specific properties
// of each or the object itself. This is really cool!
fn dailyReport(insectReport: []Insect) !void {
    std.debug.print("Daily insect report:\n", .{});
    for (insectReport) |insect| {
        insect.print();
    }
}

// Interfaces... (explanation from Dave)