Hacker News new | ask | show | jobs
by cornstalks 15 days ago
You can do a+b in Zig with SIMD vectors. No operator overloading required
2 comments

Sure. But doesn't it still lack autodiff, quaternions, etc?
In C++, the practical AD approach for large existing code-bases is operator-overloading. Swapping double for an active class, overload all math operations for this type, recording the computational graph on a tape, and then run the tape backwards for computing derivatives. This only works because operator-overloading and templates are language features.

Zig has no operator overloading, so tape-based AD means rewriting arithmetic as functon calls. Its compile-time code gen could instead do source transforamtion (like Enzyme / Tapenade, etc), which is a different trade-off.

Concrete example: With operator-overloading in C++, the Black-Scholes d1 expression, a statement with 5-inputs and 10 math operations, can be recorded with 10 nodes and operand links on tape. If adding expression templates, this reduces to a single tape entry function call, the rest is handled by the compiler. And the code is still readable like the original with double type.

Yes, there are definitely other use cases that Zig doesn't support (which some may view as a good or bad thing). I just wanted to respond to the comment about basic SIMD since the example was U8x32 and operator+, which Zig can do.
Point wasn't about SIMD, but defining my wrapper for it and giving it operations I needed.

Which seems to be something Zig can't do.

Hell, you can define operators for generic length SIMD lane.

It's not that Zig can't do it; Zig has been carefully designed to prevent it from being done as one of its core values, which is something that's important to some people just as the ability to do it is important to some other people. Supporting it or preventing it both have pros and cons for different users and different uses.
Sure Zig is pre-1.0 it can do anything but it's not going to do anything (e.g. add a borrow checker that closes the possible memory unsafety issues. They will add a runtime checker via Fil-C though)

The issue is the mindset, "I (Zig creator) don't need it nobody else does."

That's not what I wrote. I said Zig is carefully designed to disallow overloading because that's what some of us want (and there are Zig-specific reasons to want that even more). You, however, seem to be of the mindset that if some set of people want a feature, every language should have it, but that is clearly untrue. Something many of us value (especially in low-level programming, but not just there) is simplicity, which is the one feature you can never add to a language once you lose it. In every product, including programming languages, what features you don't allow in is just as important as the features you have. What made the iPhone the iPhone wasn't that it had a touchscreen, but that it didn't have a physical keypad.

In the case of Zig this is especially glaring and a core aspect of its design. In my low-level work, partial evaluation (or "compile-time code generation") is very important, and both C++ and Rust have it, albeit inelegantly. The problem is that they both bring with them the complexity I try to avoid. Zig is special because it offers all that power in a simple language, which is what many people want.

Different programmers want contradictory things out of programming languages, and it's impossible to satisfy all of them. Some people want to use a language with operator overloading, while others want to use a language that doesn't offer operator overloading. They can't be using the same language.

Same goes for any bitwise operation <<, >>, &, |, %.

Can I write my SIMD/number abstraction and have it work?

Let's say I need rotors. With six dimensions. Can I overload it's basic numeric operations?

You need to understand that the things you want to do are the very things that others want their language to ensure cannot be done. For example, the question I'm asking is, can your preferred language ensure that operators are not overloaded? You want a language with an operator overloading feature, and I want a language with an operator-overloading-prevention feature. My preferred language lacks the feature you want, and your preferred language lacks the feature I want. The same language cannot give us both what we want, because what we want is contradictory. This is why we prefer different languages; some cater to your preferences and others to mine.