20 lines
520 B
Zig
20 lines
520 B
Zig
const std = @import("std");
|
|
|
|
pub fn Lazy(comptime T: type, comptime init_fn: std.meta.DeclEnum(T)) type {
|
|
return struct {
|
|
const Self = @This();
|
|
|
|
value: ?T = null,
|
|
|
|
pub fn get(self: *Self) *T {
|
|
return if (self.value) |*v| v else blk: {
|
|
self.value = @field(T, @tagName(init_fn))();
|
|
break :blk &self.value.?;
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
pub fn lazy(comptime T: type, comptime init_fn: std.meta.DeclEnum(T)) Lazy(T, init_fn) {
|
|
return .{};
|
|
}
|