#Destructuring Tuples?

1 messages · Page 1 of 1 (latest)

oblique halo
#

Does Zig have any way of destructuring tuples, assigning names to them ad-hoc, or any kind of thing similar to this?

fn load_player(state: ona.StateTuple(&.{PlayerBody, Assets})) coral.io.AllocationError!void {
    state[0].?.texture = state[1].?.load_texture("player.png");
}

I don't see this as particularly readable and writing out local variables for each instance of a logic system in my project seems repetitive at best.

I expect given how Zig treats anything that can be done using existing syntax as unnecessary to the language there probably isn't any destructuring syntax sugar in the language, but is there any way to bind a tuple to named fields in a new struct easily?

vague sun
#

Not sure if it meets your exact use case but the recently added (master/v0.12 only) tuple destructuring syntax is as follows:

const my_bool: bool, const my_int: i8 = .{ true, 17 };

Unfortunately, that doesn't allow for destructuring from an anonymous struct (tuple) to a normal struct. However, that might be possible with a little reflection if you're able to iterate over the tuple's fields, though I haven't tried that myself.

oblique halo