#Concise way to instantiate generic types?

1 messages · Page 1 of 1 (latest)

weak rover
#

What's the accepted way of doing something like this? The current code is a bit too verbose for my liking.
I've read about (decl literals)[https://github.com/ziglang/zig/issues/9938] but it seems those are stuck in limbo for perpetuity.

GitHub

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software. - Issues · ziglang/zig

abstract berry
#

You could only make it shorter by using an unmanaged hashmap (no allocator; can use T{} syntax to init it), or shorten the names with things like const V3 = vector.Vector3;

weak rover
#

thanks, I think i'll just do the name shortening stuff, shame it all has to be typed out manually

valid shuttle
#

I will just point out: std.hash_map.AutoHashMap -> std.AutoHashMap

#

and also, you oculd just shorten the generic type name itself:

sections: SectionsMap,

const SectionsMap = std.AutoHashMap(vector.Vector3, section.Section);

pub fn new() World {
    return .{
        .sections = SectionsMap.init(...),
    };
}
#

besides that, I would recommend against using std.heap.page_allocator directly, unless you want to have very slow code

#

rather you should accept an allocator: std.mem.Allocator parameter