#Copy (all or filtered) values from another instance of the same struct

1 messages · Page 1 of 1 (latest)

limpid thunder
#

One way would be something like

fn update(comptime T: type, a: *T, b: anytype) void {
  inline for (@typeInfo(@TypeOf(b)).Struct.fields) |field| {
    @field(a, field.name) = @field(b, field.name);
}
#

and youd call it like
update(&s, .{.field1 = 10, .field2 = "foo"});

#

Or you could have the update function take a *T, a T, and a list of fields to copy