Personally, I would prefer an implementation where vectors are declared in the manner somewhat like that of a struct:
const Pixel = vector(f32) { r, g, b, a };
const p: Pixel = .{ .r = 0, .g = 0, .b = 0, .a = 1 };
p.a = 0.5;
Elements in a vector generally have specific meaning. For a pixel, the first element is the red channel, the second is the green channel, and so on. Vectors are generally not used in the same manner as arrays. They aren’t collections of things of the same type. The ability to reference elements by name is very helpful.
Potentially we can have convenient syntax like this:
p1.rgb = p2.rrr;
Which sets the first three vector elements of p1
to the first element of p2
.
EDIT: Here’s perhaps a better example. Instead of this:
pub const Vector = @Vector(3, f32);
pub fn cross(v1: Vector, v2: Vector) Vector {
const p1 = @shuffle(f32, v1, undefined, @Vector(3, i32){ 1, 2, 0 }) * @shuffle(f32, v2, undefined, @Vector(3, i32){ 2, 0, 1 });
const p2 = @shuffle(f32, v1, undefined, @Vector(3, i32){ 2, 0, 1 }) * @shuffle(f32, v2, undefined, @Vector(3, i32){ 1, 2, 0 });
return p1 - p2;
}
We would have something like this:
pub const Vector = vector(f32) { x, y, z };
pub fn cross(v1: Vector, v2: Vector) Vector {
return v1.yzx * v2.zxy - v1.zxy * v2.yzx;
}