r/ProgrammingLanguages • • Jan 24 '26

Discussion Why don't any programming languages have vec3, mat4 or quaternions built in?

Shader languages always do, and they are just heaven to work with. And tasty tasty swizzles, vector.xz = color.rb it's just lovely. Not needing any libraries or operator overloading you know? Are there any big reasons?

110 Upvotes

132 comments sorted by

View all comments

Show parent comment

1

u/protestor Jan 24 '26

What's special is the syntax, something.xy and something.yx having the same fields but reversed (in such a way that something.xy = .. and something.yx .. work).

There's some languages that can implement them though: you need a method to resolve any missing field. Such method would receive "xy" or "yx" as a string, and decide what to do with this. I think Javascript is one of those languages (with proxy obects). Ruby probably is too.

However swizzles are often used in performance-sensitive code, and it's challenging to make the above implementation fast, unless the code that decides what to do with arbitrary field names runs at compile time

3

u/initial-algebra Jan 24 '26

For a vector with a finite number of components, and assuming you don't allow duplicate components (for which assignment would be nonsense anyway), you can enumerate all possible swizzles up front and generate a getter/setter or whatever for each of them.

1

u/protestor Jan 24 '26

Getter/setter is still a step down from just a field, in terms of usability. But many more languages support having a field being implemented in terms of a getter and setter, which is reasonable

Also native swizzles are probably easier to optimize (but I'm not sure, maybe inlining the getter and setter is enough)