Skip to content

Instantly share code, notes, and snippets.

@lithdew
Created September 14, 2020 09:30
Show Gist options
  • Save lithdew/1e930ac87ad554e066139d024fe23ff8 to your computer and use it in GitHub Desktop.
Save lithdew/1e930ac87ad554e066139d024fe23ff8 to your computer and use it in GitHub Desktop.
zig: inheritance
pub const B = struct {
const Self = @This();
data: []const u8,
pub fn getParent(self: *Self) *A {
return @fieldParentPtr(A, "child", self);
}
};
pub const A = struct {
child: B,
};
pub fn main() !void {
var a: A = .{ .child = .{ .data = "I am A!"} };
var b: A = .{ .child = .{ .data = "I am B!"} };
const std = @import("std");
std.log.info("A: {}", .{a.child.getParent()});
std.log.info("B: {}", .{b.child.getParent()});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment