← Back to context

Comment by pixelpoet

10 months ago

For one thing, C3 thankfully understands that there are more mathemathical types people are interested in than just ints and reals, for example vectors: https://c3-lang.org/language-common/vectors/

Zig has SIMD vectors, but I frequently need 3D vectors, and refuse to use things like vec3_add(vec3_mul(a, 2), b) etc since I mainly develop 3D graphics software.

In Zig, basic operations on vectors with standard operators work fine. "+", for example.

  const meta = @import("std").meta;
  
  test "vector add" {
     const x: @Vector(4, f32) = .{ 1, -10, 20, -1 };
     const y: @Vector(4, f32) = .{ 2, 10, 0, 1 };
     const z = x + y;
     try expect(meta.eql(z, @Vector(4, f32){ 3, 0, 20, 0 }));
  }

Everything is element-wise, which matches what the shading languages do (mostly).

But, yes, you won't get overloading allowing things like dot, cross, or scalar products. And I do miss the swizzle notation, but I don't think the language actually prevents that so it might appear at some point.

  • C3 allows arbitrary extensions of any types, so things like `vec1.dot(vec2)` is actually implemented as a generic method macro over all vector types added by the math module.

    Given Zig's preference for closed modules, I don't expect this to be on the roadmap, but rather would need to be implemented as functions.

interesting example of swizzling

``` int[<3>] a = { 11, 22, 33 }; int[<4>] b = a.xxzx; ```

I assume that the `xxzx` is translated directly by the compiler. not seen that in any other language though ruby can fake it pretty easily via `method_missing`