hi,
in odin i could do:
Ressource :: struct {
variant: union{
^FontAsset,
}
}
FontAsset :: struct {
using base: Ressource,
}
RessourceCache :: struct {
map_ressources: map[string]^Ressource
}
load_asset::proc($T: typeid, self: ^RessourceCache, path: string) -> ^T {
// here, great
asset: ^T = self.ressources[path].variant.(^T)
return asset
}
in zig, i try to achieve something similar:
pub const Ressource = struct {
variant: union(enum) {
font: *FontAsset,
},
};
pub const RessourceCache = struct {
map_ressources: std.StringHashMap(*Ressource),
pub fn load(self: *RessourceCache, comptime T: type, path: []const u8) ?*T {
var existing = switch (self.map_ressources.get(path).?.variant) {
.tex => |val| val,
.font => |val| val,
.model => |val| val,
};
return asset;
}
};
This is the best i could come up with, even thought i don't like the switch.. and it doesn't compile:
error: incompatible types: '*assets.TextureAsset' and '*assets.FontAsset'
var existing = switch (self.map_ressources.get(path).?.variant) {
^~~~~~
Is there a better way?
Thanks