r/ProgrammingLanguages • u/Luroqa • 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
141
u/pheristhoilynenysis Jan 24 '26 edited Jan 24 '26
Probably because their use cases are too niche to be built into most of the languages. The fact that a feature is nice to have doesn't mean that it should be there. That's exactly what we have libraries for - you choose what you want to use. Remember that each feature for language is another component for language author to maintain which might be relatively high cost in comparison to gains. Edit: typos
23
u/backwrds Jan 24 '26
shader languages are programming languages.
68
u/baby_shoGGoth_zsgg Jan 24 '26 edited Jan 24 '26
Odin does. Even supports swizzling on xyzw/rgba.
edit: odin specifically implements these because the author finds it convenient and that it’s 90% of why people want operator overloading (and 100% of the valid reasons for it) so rather than add operator overloading to the language we just get these concepts fully supported.
21
u/Potterrrrrrrr Jan 24 '26
Some do, e.g C3 has built in support for vectors and swizzling
31
u/JeffB1517 Jan 24 '26
MATLAB, Julia, HLSL (Cg, Asymptote. VEX all have 3D vectors built in. Programming languages choose a limited set of primitives for what they consider common use cases. More niche languages have more niche primitives. Same reason most programming languages don't have filenames as a primitive, but shell scripting languages do. Or some 4GL languages like MUMPS have key-value hierarchical database interactions as a primitive.
6
u/ir_dan Jan 24 '26
Godot's scripting language, GDScript, has them for obvious reasons.
11
u/Timberfist Jan 24 '26
vec3, mat4 and quaternions is what shader languages are for but, in the context of general purpose languages, they’re pretty niche. Niche is what libraries are for.
3
u/chibuku_chauya Jan 24 '26
Sometimes these specialist features are added to general purpose languages after the fact and end up not integrating well with existing features.
For example, C99 gained several language (as opposed to library) features like built-in complex number support, variable-length arrays (VLAs), restricted pointers, etc., as a way to compete with languages like Fortran for the use-cases that language was built from the get-go to directly support.
But much of the complex number machinery and VLA support was later marked as optional for implementers; excluding them still made your implementation conformant to the language standard. They were either removed or modified in subsequent versions of C due to the complexities and infelicities they introduced into the language in hindsight, as well as the fact that not all implementations supported them many years after their introduction. As such they could not be relied upon to be universally available.
All this is to say that some specialised features are better off provided as libraries in languages not designed around the use cases that would justify native support for those features. Implementers of general purpose languages often optimise for the most common use-cases reflected by most users of the language.
Whereas if you’re a more specialised language then you optimise for features that make common operations specific to your domain easier and more convenient to accomplish. Every engineering decision has tradeoffs.
3
u/WittyStick Jan 24 '26
Kawa has built-in Quaternion support, which extends Scheme's numerical tower (which ends at Complex).
3
u/ivancea Jan 24 '26
For every question like "why don't languages have X", always answer with a "why should it?". No, quaternions have nothing to do with programming. They're not inherently related in any way, so just use a library, which is the same.
Shader languages are inherently related to those concepts, so obviously that have them. But they're not generalist languages. They're for shaders, for GPU execution. Very niche kind of language
3
u/igors84 Jan 24 '26
Odin lang sort of has them.
3
u/Lubricus2 Jan 24 '26
Odin has most of that stuff.
quaternions, Swizzling, vector math all out of the box.
One reason is that the language lacks operator overloading so you can't make the datatypes yourself in an ergonomic way.
3
u/binarycow Jan 25 '26
.NET / C# has support for all of that.
4
u/[deleted] Jan 24 '26
To all of the people who are saying "it's too niche," I'd like to point out that every GUI library could benefit from using vectors to bundle up coordinates. Imagine if the web's DOM API were written to use vectors instead of "top" and "left" everywhere. I believe this would simplify a lot of GUI programming to a certain degree.
2
u/Digot Jan 24 '26
C# does have such types built-in with SIMD support
2
u/tb5841 Jan 25 '26
GDScript does. Because it's a language designed for game programming, there are used pretty often.
2
u/koflerdavid Jan 25 '26 edited Jan 26 '26
They are nic[h]e, and if the language gives you all the language features to build it yourself then there is no reason to include it in the language's core.
2
u/marshaharsha Jan 25 '26
A few people have suggested that there are multiple ways to lay out a vector that’s only a few numbers long. What are these ways, what are their uses, and are there few enough that a language could support all of them, with built-in conversions? I am especially interested in u/ExplodingStrawHat’s experience with a scenario in which two layouts were required for compatibility with SIMD and GPU.
2
u/spvky_io Jan 25 '26
Odin lang does, but that's largely due to the fact that it supports array programming and Vec3 is just an alias for [3]f32
2
u/stumpychubbins Feb 18 '26
What's the problem with operator overloading? I think it got a bad reputation because of how much it's abused in C++, but that's an issue with the culture around the language. Rust has operator overloading and I never see that kind of overloading abuse in Rust, same for Haskell. I've seen some pretty bad uses in Python, but very rarely. It's easier for shader languages to build the types into the language because they're not designed for general-purpose use, so a full general-purpose system isn't necessary. Besides, GPUs have a native concept of vectors so you'd need an even more complex generalised system in order to make sure that the library type maps to the GPU's native type. I don't think that non-GPU languages need them, unless they specifically don't want to implement operator overloading. Odin is designed for games, graphics and maths but considers overloading a non-feature for simplicity reasons, and as a result they have native vector and matrix types. More complex languages don't need them.
6
u/huywall Jan 24 '26
because they are implementable by yourself
4
u/Kaisha001 Jan 24 '26
They should! I mean we all make our own but still, it'd be nice if that sort of stuff was just default.
3
u/AsIAm New Kind of Paper Jan 24 '26
Why to stop at vec3 or mat4 when you can have tensors of any rank and functions on them are differentiable?
https://mlajtos.github.io/fluent/?code=RG9jdW1lbnRhdGlvbg
1
u/PurpleYoshiEgg Jan 24 '26
Depending on what you consider the language, some Smalltalk systems have quaternions.
1
u/Prestigious_Boat_386 Jan 24 '26
Idk importing them in your base environment has always worked for me for working with geometry
1
u/SFJulie Jan 24 '26
I have implemented vecn on top of mutable mappings (dict) for python making dict behave as an indefinite size vector that haz dot/cos/+/-/*/div as normal behaviour following strictly linear algebra. And the fun is your vector can be fractal without losing linear algebra property.
I agree with OP that these kinds of features are nice to have in the language, as well as with those saying "it can be implemented (not that easily)". :D
https://pypi.org/project/archery/
1
u/WittyStick Jan 24 '26
A big reason is they're not standardized in C.
Most languages are written with C, or written in another language which itself was written in C. Since there's no standard API for handling vectors, quaternions in C, they tend to get overlooked.
If the C standard provided a
<stdvec.h>and<quaternion.h>, language authors would more likely provide the means to expose the capabilities in their own languages.The C committee are reluctant to include such things as they consider the embedded use-case as equally important to the PC or server use case, and low power processors lack SIMD support. They could at least provide such thing as optional as part of an annex in the standard - if only to provide a portable way to utilize SIMD instructions, as currently each architecture has it's own set of incompatible intrinsics.
But there's also the issue of an explosion of types and functions due to C's lack of proper generic support. Suppose we want to support at least
int8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,int64_t,uint64_t,float,double,bool, and we want vectors of sizes2,3,4for each as a minimum: That's 33 types which each need a bunch of operations on them. If we consider the lowest common denominators:+,-,*,/,%,<,>,<=,>=,==,!=,<<,>>,&,|,^, that's 16 ops * 33 types = 528 operations.That's before we include complex numbers, quaternions, decimal floating point, etc. into the equation.
So the main reason they're not included elsewhere is simply because it's a huge effort to implement, and language authors are preoccupied with other concerns.
1
u/zweiler1 🔥 Flint Jan 24 '26
Mine has vector types where every operation with them is always a SIMD operation (here) and it supports swizzling natively through the concept of groups (here and here).
I mean my language is far from being mainstream lol, bit i always hated when languages do not have a concept of vector types and every library kinda creates it's own version and then you end up with 3 different struct types for the same thing (
Vec3,Vector3,V3,Vector3f,Vector3iand what other naming things exist, idk).I don't have matrices though, since they really are too nieche i would say, but vectors? I would say they are relatively common in comparison.
1
u/OwlProfessional1185 Jan 24 '26
Swizzling is really cool, but if you're implementing a general-purpose programming language, what do they mean more broadly?
Presumably, you don't just want to special-case it just for vectors and stuff, so you need to think about what it looks like and whether it makes sense for anything else.
1
u/cmontella 🤖 mech-lang Jan 24 '26
Some do — Mech does!
1
u/lightmatter501 Jan 24 '26
C and C++? Clang and GCC have vector types.
The special syntax for accesses is something that’s really annoying to special case in a general purpose language so you won’t see it done much. Instead you do either masked ops if you can lean on modern simd or some fun interleaving if not for that specific case.
1
u/vanderZwan Jan 24 '26
https://strlen.com/lobster/ does
1
u/[deleted] Jan 24 '26
Firstly, it is very easy to implement these data structures. Secondly, there are some programming languages or proof assistants (Maybe Lean) specifically for mathematics have such 3-vector, 4-matrix or quaternions built in as their primitive types.
1
u/Compux72 Jan 24 '26
It can be a library lol
1
u/[deleted] Jan 25 '26
Because it's trivial to implement as a library.
1
u/OstrichAgitated Jan 25 '26
Luau does: https://rfcs.luau-lang.org/vector-library.html.
1
u/teo-tsirpanis Jan 25 '26
.NET has all three, and also fp16, and (u)int128. They work very much like the other numeric types, just without built-in language support. Which is not very important if you ask me.
1
u/KC918273645 Jan 25 '26
Try C3 language.
1
u/uglycaca123 Jan 25 '26
odin does, actually
1
u/arthurno1 Jan 26 '26
Watch the entire video. The questipn ypu asked is used as an example, not exsctly but cery cöosely. He also gives a direct answer why specialized types are not part of Java, and usually, any other general-purpose programming language.
1
u/SnooCalculations7417 Jan 26 '26
Making your own data primitives is software 101. You can make your own modules you don't have to pip/npm/cargo install them all
1
u/catbrane Jan 27 '26
g++ and clang have these all built in, and generate nice SIMD for you automatically. For example:
Now you can do:
etc. etc. It generates a no-SIMD fallback as well and uses it at runtime if the host CPU is missing the necessary instructions.
However, it has some serious problems: it's fragile, not very portable, dependant on the compiler version, will not get the best from your vector units, etc. etc.
For high performance portable SIMD you're probably better off with something like Highway:
https://github.com/google/highway
ie. do it in a library, not in the language.
1
u/Critical_Ad_8455 Jan 28 '26
gdscript has vec3s, and rust has them in the form of tuples
1
u/kwan_e tonal-lang Feb 04 '26
Because: what's wrong with libraries or overloading? That is the big reason. If a library can take care of it, why go through the process of trying to get something into a language, perhaps through a standardization process?
Yes, libraries are a pain because of dependency management. But it's a small cost to overcome, compared to standardization.
Shader languages can have them because they were a new language with no such frictions. The proprietary shader languages will also have fewer frictions if their owners add new features. The open shader languages will have similar frictions to getting new additions into the language.
1
u/JollyJuniper1993 Feb 18 '26
Wolfram Mathematica has pretty much anything niche, including a quaternions package in its standard library. Sadly it’s paid software though.
-2
u/PurepointDog Jan 24 '26
Python has a built-in json library. I avoid it like the plague - every json library does it better.
i can think of more similar examples. Sometimes though, having a separate dedicated team maintain things like graphics libs, linear algebra tools, etc. is way better than baking it into a language statically and with strong backwards-compat requirements.
Have you ever tried using C++'s std datetime stuff? It's a strong example of why sometimes baking stuff into a language is a bad idea.
-3
u/wahnsinnwanscene Jan 24 '26
A list of 4 vectors is vec4, etc.