Zig NEWS

Discussion on: zoltan: a minimalist Lua binding

Collapse
 
lhp profile image
Leon Henrik Plickat

However, I feel that managing shared memory/resources (std::shared_ptr) could be problematic; and hard-to-maintain code may emerge in the future.

Depending on the problem you are trying to solve, you can likely just do ref-counting in most cases.

You can "externalize" the reference counting mechanism in zig as well:

fn RefContainer(comptime T: type) type {
    return struct{
        inner: T,
        refs: usize,

         const Self = @This();

         pub fn init(alloc: mem.Allocator, inner: T) !*Self {
             // If T has init method, call that.
             // ...         
         }

         pub fn ref(self: *Self) *Self {
             // ...         
         }

         pub fn unref(self: *Self) void {
             // If self.refs == 0 and T has deinit method, call that.
         }
    };
}
Enter fullscreen mode Exit fullscreen mode