←back to thread

Zig is hard but worth it

(ratfactor.com)
401 points signa11 | 4 comments | | HN request time: 0.028s | source
Show context
jsheard ◴[] No.36150389[source]
I get what Zig is going for in making all operations as explicit as possible, but I fear that it's going to turn away fields like graphics and game development where it would be a good fit except for the lack of operator overloading forcing you to go back to C-style math function spaghetti. It's all fun and games until what should be a straightforward math expression turns into 8 nested function calls.
replies(8): >>36150475 #>>36150541 #>>36150795 #>>36150946 #>>36151013 #>>36151746 #>>36151792 #>>36152172 #
TwentyPosts ◴[] No.36150541[source]
I am not much of a Zig-head, but the best compromise I can think of is having a few operators which purely and solely exist for this purpose. In other words, there is no operator overloading, but you can define, say, "#+" for any two structs, or something like that.

So if you want to encode matrix multiplication, then you'll always have to write `mat1 #* mat2`. This feels like a hack, and isn't all that elegant, but it'd be clear that every usage of such an operator is a disguised function call. (And according to what Andrew Kelley said, it's all about not hiding function calls or complex operations in seemingly innocent 'overloaded' symbols.)

If you want to take this one step further you'd probably have to allow users to define infix functions, which would be its own can of worms.

Honestly, I am not particularly happy with any of these ideas, but I can't think of anything better either!

replies(3): >>36150784 #>>36153058 #>>36153273 #
thechao ◴[] No.36153273[source]
Maybe an operator-overloading region?

    #{
       m3 = m1 * m2 + m3;
       m3 += m4;
    }
Basically, pure syntactic sugar to help the author express intent without having to add a bunch of line-chatter.

Speaking of operator-overloading, I really wish C++ (anyone!) had a `.` prefix for operator-overloading which basically says "this is more arguments for the highest-precedence operator in the current expression:

    a := b * c .+ d;
Which translates to:

    a := fma(b, c, d)
replies(1): >>36154201 #
1. MH15 ◴[] No.36154201{3}[source]
Huh I've never seen this approach. Very interesting solution, could be adapted to the JavaScript matrix libraries I bet.
replies(3): >>36154476 #>>36154965 #>>36157284 #
2. thechao ◴[] No.36154476[source]
This is how TeX handles math — the "$" operator is an "inline" version of the same.
3. estebank ◴[] No.36154965[source]
In Rust you could use a proc macro that parsed the block and translates the token to a new token stream that uses method calls with the appropriate operator precedence, for arbitrary operations you could want to define. You're effectively writing a compiler plugin and language extension at that point. For targeted niche domains, this might be worthwhile.
4. bakkoting ◴[] No.36157284[source]
There's a not-very-active proposal to add operator overloading to JS which takes a similar scoped approach:

https://github.com/tc39/proposal-operator-overloading