#declaration literals

1 messages · Page 1 of 1 (latest)

gaunt depot
#

Am I right that using Zig's new declaration literals, you can't override the defaults when initializing the variable?

var gpa = std.heap.GeneralPurposeAllocator(.{}){
    .backing_allocator = ...,
};

vs

var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;  
gpa.backing_allocator = ...;  // is this the only way?
keen forge
#

yeah, .init here just reference a declaration made inside of the struct, nothing more. it’s not like a special init syntax

inland lagoon
#

Aren't you able to pass .{ .thread_safe = false } into the config argument of the gpa?

gaunt depot
#

err..ya, my mistake

inland lagoon
#

.init should then be fine to use

gaunt depot
#

What about backing_allocator, for example, though ?

#

I changed the original post/question

inland lagoon
#

Then yeah, I think you'll need to either initialize with the init decl and then change the backing allocator or initialize each field yourself

lethal notch
#

yeah basically

#

not being able to do this is one of the reasons for decl literals existing

inland lagoon
#

Sinon is correct that there's nothing special about .init, it's just a constant declared on GPA

lethal notch