Dynamic dispatch and vtables. Problem accessing types

I’ve been trying to create a system where i can store different types of data as “bundles”. The input order should not matter (i sort the input by their type), and i should be able to get only parts of the data.

I basically want it to work like this

test "test" {
    const allocator = testing.allocator;

    const Struct1 = struct { x: u32, y: u32 };
    const Struct2 = struct { x: u16 };
    const Struct3 = struct { x: u64 };

    const BundleTypes1 = .{ Struct1, Struct3, Struct2 };

    var bundles = try Bundles.init(BundleTypes1, allocator);
    defer bundles.deinit();

    bundles.add(.{ Struct3{ .x = 64 }, Struct1{ .x = 32, .y = 32 }, Struct2{ .x = 16 }});

    _ = bundles.get(.{ Struct1 Struct3 }).iterator();
}

I’ve got it to mostly working. I sort the types and create some meta data about the offset and size of each type contained in the bundle type when i create the bundle. I can add a bundle with the types in any order. The problem comes when i want to get the data. The metadata is runtime and the input types is comptime so i can’t sort the types based on metadata to later cast the output(the bundle is stored as []u8 in memory). I also might not have all the types when getting the data and can therefore not re-generate the metadata.

Making the metadata comptime would solve the issue. I’ve tried to use a vtable to do this and this enables me to get access to the metadata at compiletime.

pub const VTable = struct {
    add: *const fn (usize, *Bundles, usize) void,
};

pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
    const ObjectType = Object(type_bundle);
    const BundleType = @TypeOf(type_bundle);
    const object = try allocator.create(ObjectType);
    errdefer allocator.destroy(object);
    const bundles = Bundles{
        .allocator = allocator,
        .object = @intFromPtr(object),
        .vtable = &comptime VTable{
            .add = struct {
                fn add(ptr: usize, ctx: *Bundles, bundle_ptr: usize) void {
                    // I no longer have access to the anytype of the bundle
                    // Since you cant have anytype in the vtable
                    const self: *ObjectType = @ptrFromInt(ptr);
                    const bundle: *BundleType = @ptrFromInt(bundle_ptr);
                    @call(std.builtin.CallModifier.always_inline, ObjectType.add, .{ self, ctx, bundle.* });
                }
            }.add,
        },
    };
    object.* = try ObjectType.init();
    return bundles;
}
pub fn BundleObject(bundle_types: anytype) type {
    const meta = GenerateBundleMeta(bundle_types, bundle_types.len, PackingStrategy.Aligned);
    return struct {
        const Self = @This();
        const BundleType: type = @TypeOf(bundle_types);
        const ObjectMeta: @TypeOf(meta) = meta;
        fn add(self: *Self, ctx: *Bundles, bundle: anytype) void {}
    };
}

This creates some other problems where i no longer have access to the “anytype” of the bundle.

So i’ve in desperation tried to do stuff like this as well:

vtable: *const GenerateVTable(VTableFunctions),

pub fn GenerateVTable(comptime functions: type) type {
    const BuiltinType = std.builtin.Type;
    const table_info = @typeInfo(functions);
    comptime var tfields: [table_info.Struct.fields.len]BuiltinType.StructField = undefined;
    for (table_info.Struct.fields, 0..) |field, ii| {
        if (!field.is_comptime) @compileError("Function needs to be comptime");
        tfields[ii] = BuiltinType.StructField{
            .name = field.name,
            .alignment = field.alignment,
            .type = field.type,
            .is_comptime = field.is_comptime,
            .default_value = field.default_value,
        };
    }
    const decls: [0]BuiltinType.Declaration = undefined;
    return @Type(.{
        .Struct = .{
            .layout = BuiltinType.ContainerLayout.auto,
            .backing_integer = null,
            .fields = &tfields,
            .decls = &decls,
            .is_tuple = false,
        },
    });
}
const VTableFunctions = struct {
    comptime add: fn (*Bundles, anytype) void = struct {
        pub fn add(self: *Bundles, bundle: anytype) void {
            std.debug.print("Me add: {}n", .{bundle});
            _ = self;
            // self.object.add(bundle); <-- no type to cast to
        }
    }.add,
};

pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
    // ..
    const bundles = Bundles{
        //..
        .vtable = &comptime GenerateVTable(VTableFunctions){},
    }
    return bundles;
}

Now i have access to the bundles “anytype” but i can no longer generate and store the metadata at comptime.

So i thought i might be able to do this:

pub fn init(type_bundle: anytype, allocator: Allocator) !Bundles {
    // ..
    const me = Bundles {
    //..
        .vtable = &comptime GenerateVTable(VTableFunctions){
            .add = struct {
                pub fn add(self: *Bundles, Entity, bundle: anytype) void {
                    // ..
                }
            }.add,
        },

    }
// ..
}

But this gives me this error: error: value stored in comptime field does not match the default value of the field. I kind of knew this already, but yeah… desperation.

I’m kind of new to the c-style of programming and since zig does not have traits or interfaces this kind of problem is kind of new to me.

One of the easiest solutions would maybe be to make Bundles a genereic but then I’ll just move the complexity to other locations. For example how do i store hundreds of different bundles when each bundle is it’s own type (Bundle(.{Struct 1}) vs Bundle(.{Struct2)? Nah i don’t want this.

What Am i missing here, what can i do to archive my goal?
Thanks in advance for any help you can provide.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật