Created
September 14, 2020 09:30
-
-
Save lithdew/1e930ac87ad554e066139d024fe23ff8 to your computer and use it in GitHub Desktop.
zig: inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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