In the ecs I am working on I currently have code like this:
fn drawRectanglesLines(self: *System, archetype: *Archetype) !void {
const c = try self.input(archetype, struct {
position: Vec2,
bounds: Vec2,
color: ray.Color,
});
for (c.position, c.bounds, c.color) |pos, b, color| {
ray.drawRectangleLines(pos[0] + 1, pos[1] + 1, b[0] - 2, b[1] - 2, color);
}
}
If there was a special for <tuple> |a, b, c| { ... }
I could write this instead:
fn drawRectanglesLines(self: *System, archetype: *Archetype) !void {
const c = try self.input(archetype, struct {
position: Vec2,
bounds: Vec2,
color: ray.Color,
});
for c |pos, b, color| {
ray.drawRectangleLines(pos[0] + 1, pos[1] + 1, b[0] - 2, b[1] - 2, color);
}
}
Or even this (if I don’t care about verifying component order/names):
fn drawRectanglesLines(self: *System, archetype: *Archetype) !void {
for try self.input(archetype, .{Vec2, Vec2, ray.Color}) |pos, b, color| {
ray.drawRectangleLines(pos[0] + 1, pos[1] + 1, b[0] - 2, b[1] - 2, color);
}
}
This would work because System
has a slice of self.component_ids
internally.
Maybe to avoid unreadable expression monstrosities within such a hypothetical tuple-for, it could instead accept a variable name, which would force you to separate that code out:
fn drawRectanglesLines(self: *System, archetype: *Archetype) !void {
const components = try self.input(archetype, .{Vec2, Vec2, ray.Color}); // returns tuple
for components |pos, b, color| {
ray.drawRectangleLines(pos[0] + 1, pos[1] + 1, b[0] - 2, b[1] - 2, color);
}
}
I would like that tuple-for possibility, but what I suggest would be a new kind of syntax we haven’t had so far (dropping the parentheses from a syntactic form), but I like it because it seems very readable.