const std = @import("std");
const Animal = struct {
pub fn talk(self: anytype) void {
self.talk();
}
};
const Cat = struct {
anger_level: usize,
pub fn talk(self: Cat) void {
std.debug.print("[Cat] Meow! (anger Lv {})\n", .{self.anger_level});
}
};
const Dog = struct {
name: []const u8,
pub fn talk(self: Dog) void {
std.debug.print("[Dog] I'm a {s}\n", .{self.name});
}
};
const Panda = struct {
pub fn talk(_: Panda) void {
std.debug.print("[Panda] Hahaha!\n", .{});
}
};
fn do(self: anytype) void {
self.talk();
}
pub fn main() !void {
const cat = Cat{ .anger_level = 100 };
Animal.talk(cat);
const dog = Dog{ .name = "lilly" };
Animal.talk(dog);
const panda = Panda{};
Animal.talk(panda);
do(cat);
do(dog);
do(panda);
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme