Vector, Matrix, and Quaternion Types

Note: It is TBD whether this will be a core language feature, or a library one. Also TBD on whether there’s a way to represent all this via arrays above.

Since so many programs implement their own “Point” type, I’ve decided to add builtin vector types (at least in stdlib). Matrix & quaternion types are also likely to be present for completeness.

Syntax is as of yet TBD. A generics-like syntax would be ideal for vectors (is presented here), but may cause ambiguities, and it is conceptually ugly (as it overloads the base types). Only some base types would be supported (depending on type), most notably various floats and (in most cases) integers. The dimension would be compile-time (use arrays for runtime).

Also TBD: Some way of encoding GLSL’s genType (functions that support vectors of arbitrary size or scalars).

Quaternions

// imaginary numbers (ifloat); feature TBD
ifloat iv = 3.2i;
// complex numbers
cfloat cv = 5.0 + iv;	// 5.0 + 3.2i
var n = cv.real * cv.imag;	// (or cv.re * cv.im, or cv.r * cv.i: TBD)
// quaternions
qfloat qv = cv + 0.5j - 1.5k;	// 5.0 + 3.2i + 0.5j - 1.5k
// used as something like qv.r, qv.i, qv.j, qv.k

Vectors

var a = float#4(1.5, 0, 0, 0.3);
var b = int#4(2);	// int#4(2, 2, 2, 2)
var c = int#4(a) * b;	// assert(c == int#4(2, 0, 0, 0))
// if above syntax is undesired, the language supports aliases; for example, for GLSL-like types:
alias vec4 = float#4;
alias ivec4 = int#4;
var a = vec4(1.5, 0, 0, 0.3);

// swizzling is supported, with same semantics as GLSL: (refer to that for details)
assert(a.xyzw == a.stpq == a.rgba);
assert(a.xxx == float#3(a.x, a.x, a.x));
assert(a.wyxy == float#4(a.w, a.y, a.x, a.y));
// so is indexing & slicing (TBD: return type for slicing?)
assert(a[1] == a.y);
assert(a[1..3] == a.yz);

Matrices

TBD: Row-major or column-major (probably the same as multidimensional arrays, though)? I’m thinking always the same in the language, but allow different internal representations (though this may affect casting to arrays).

var m1 = float#(4,3)(a, b, c);
var m2 = float#(3,3)(a.xxx, b.yyy, c.zzz);
var m3 = m1 * m2;	// TBD: m1 .* m2?
// some extension methods (created via traits)
var m4 = float#(4,4).translation(a.xyz) * float#(4,4).rotation(qv);
// same as with vectors above, we can define aliases:
alias mat4 = float#(4,4);
var m4 = mat4.translation(a.xyz) * mat4.rotation(qv);

// vector/matrix multiplication
var v1 = m4 * b;	// right side means column vector: (4x4)*(4x1) => (4x1); TBD: m4 .* b?
var v1 = b * m4;	// left side means row vector: (1x4)*(4x4) => (1x4)