logoalt Hacker News

coolThingsFirst • today at 3:52 PM • 5 replies • view on HN

show me operator overloading in Zig


Replies

pron • today at 4:07 PM

In critical low-level code, any kind of overloading - operator or otherwise - is something you'd rather not have.

By expressivity I don't mean what characters you use in the conrecte syntax, but the number of lines needed to express an algorithm (up to a small constant). C doesn't have C++'s expressivity because it lacks templates, and while you can sort of express that kind of thing with macros, it's not all the way there. Zig, however, gets you all the way there (you might need to write matrix_plus instead of +, but the expressivity is the same).

➕ show 2 replies
melodyogonna • today at 4:11 PM

Many people are against operator overloading, imo my only issue with C++ operator overloading is that you're allowed to overload the the assignment operator. From what I can see this is the root of many issues

➕ show 2 replies
ogogmad • today at 4:54 PM

Is there a way to support varying the meaning of "+" etc without introducing a lot of complexity? I suppose if Zig had nested functions, and the ability to define a nested function called "+", then you could have name shadowing instead of "overloading".

OCaml basically gives you operator overloading this way without actually doing overloading. I'm wondering why we can't have these OCaml-isms in other languages - without needing to use OCaml - which sadly comes with other commitments.

u8080 • today at 4:05 PM

please no

nvme0n1p1 • today at 3:58 PM

Yes! And exceptions and inheritance and RTTI too. Bring back all the mistakes! Let's party like it's c++99