5 ms·
Lack of support for operator overloading is a bit sad. As a result, Go can't get used in numerical applications, unless you like writing `matrix_plus(A, B)` ins
by ogogmad 3y ago
Lack of support for operator overloading is a bit sad. As a result, Go can't get used in numerical applications, unless you like writing `matrix_plus(A, B)` instead of `A + B`.
Is there a programming language that has a "simple" design like Go but which supports some operator overloading?
- didip 3y agoI mentioned this before, but I wish that Go has a Numeric interface where operator overload methods can be added and confined only for numerical objects.
- scaredginger 3y agoYou might be interested in Odin. It's very focused on numerical computing and has certainly taken some design hints from Go
- tsimionescu 3y agoAdding matrices with A+B looks nice. But then you'll soon want to do A*B+C, and you'll find that fused multiply/add is a much faster operation than doing a multiplication and then addition, and soon you'll either be writing mul_add(A, B, C) anyway, or you'll overengineer a complex solution to make A*B return some kind of object that can recognize the subsequent + operation etc. Update: corrected * representation
- ogogmad 3y agoRETVRN TO GEOMETRY https://en.wikipedia.org/wiki/Planar_ternary_ring https://en.wikipedia.org/wiki/Planar_ternary_ring If you look at the section to do with geometry, you'll see that Fused Multiply-Add T(a,m,c) is the value of y given x where y=mx+c, where the latter is clearly just a line that's not parallel to the y-axis. You may thank me now.
- microtonal 3y agoYou can have your cake and eat it. E.g. C++ expression templates allow you to fuse application of multiple operators.
- sph 3y agoYes that's the plus of using a language that supports every single paradigm ever invented. I hear that they'll add S-expressions in the next version. [1] Jokes aside, that is pretty cool, I didn't know C++ templates did that. 1: btw Lisp could do that as well, if they wanted. C++ supports thousands of syntax features, Lisp supports infinite.
- tsimionescu 3y agoPlenty of people are choosing Go because they neither have nor want the C++ cake. The OP was explicitly looking for a simple language, and C++ is the very opposite of that concept. So yes, you can have efficient nice abstractions, but only at the cost of an extraordinarily complex base language.
- microtonal 3y agoMy point was that operators don't exclude the possibility of fusion. C++ expression templates were just an example. Numba does fusion in Python.
- tsimionescu 3y agoThey don't, but require significantly more machinery in the language than just operator overloading (especially if you want it to work at compile time).
- microtonal 3y agoSure. But somebody writes it once and you can benefit from it. If you don't have operator overloading there isn't any chance of doing this. At any rate, I feel like the goalposts have been moved a bit. You originally said: But then you'll soon want to do A*B+C, and you'll find that fused multiply/add is a much faster operation than doing a multiplication and then addition, and soon you'll either be writing mul_add(A, B, C) anyway Which I just wanted to point out that you can fuse these operations with operator overloading as well. I am pretty sure that or you'll overengineer a complex solution to make A*B return some kind of object that can recognize the subsequent + operation etc. was a later edit, I don't remember reading it when I first reacted to your comment. If so, doing that is not really nice, since it breaks the flow of the reactions. At any rate, I think we agree on all the trade-offs. And typing the multiplication operator in HN syntax sucks :).
- johndough 3y agoDo fused multiply-add operations for matrices really matter? There are two relevant cases I could come up with. 1. You have large matrices. In this case, I'd think that the O(n^2) addition can be ignored, because it gets dwarfed by the the O(n^3ish) multiplication. 2. You have small matrices. In this case, the computation is likely bound by memory bandwidth. Waiting for the matrices A and B takes most of the time. Then you multiply them, which should go quickly, since the matrices are small. Then you do the addition with the matrix C, but since the product AB is already in cache and since you'd have to wait for the matrix C anyway, there is not much to be gained with a fused multiply-add.
- campbel 3y agoI'm really not a fan of small qol features like this. I think it really hurts readability for the general audience. The function itself is minimal overhead and is clearer to the reader.
- ogogmad 3y agoThe world seems like it's moving to ML. That needs matrices, complex numbers, autodiff, different fixed/floating point types. All four of these need operator overloading.
- yangl1996 3y agoYour last sentence seems fishy to me. Are you suggesting that support for C++ like operator overloading is a necessary condition for, say, autodiff? I thought Go is a Turing complete language.
- ogogmad 3y agoOperator overloading is about syntax and type safety. Brainfuck is Turing complete.
- campbel 3y agoI agree it is about syntax. But how is it about type safety?
- ogogmad 3y agoYou might want the same piece of code to work correctly under different numerical types. Answer: Op overloading + generics. E.g. Dual numbers for autodiff; 2x2 matrices of quaternions for texture mapping; matrices of differing floating point types for machine learning. Why should I implement the same matrix operations a million times over?
- campbel 3y agoCould you explain _why_ they need overloads and functions don't work? That does not immediately follow for me.
- tomcam 3y ago> Is there a programming language that has a "simple" design like Go but which supports some operator overloading? Think that one through again
- jimbob45 3y agoI think the right answer to your question would be NimLang[0]. In reality, if you're seeking to use this in any enterprise context, you'd most likely want to select the subset of C++ that makes sense for you or just use C#. [0]https://nim-lang.org/ https://nim-lang.org/
- tgv 3y agoI once had a hard time finding a bug, and it turned out someone had overloaded assignment to the boolean member dbIsOpen in some inherited monstrosity class to open/close a database connection. Since then, I prefer functions with clear names. They may still not do what their name says, but it doesn't lull you into thinking local.
- coldtea 3y ago>unless you like writing `matrix_plus(A, B)` instead of `A + B`. I am. Also, this would be trivial with a DSL and a preprocessor, so...
- __loam 3y agoZig also does it this way and I think it makes sense. Operator overloading gives the same symbols different semantic meaning in different contexts. This hurts readability and simplicity.
- munificent 3y agoCLU https://en.wikipedia.org/wiki/CLU_(programming_language) https://en.wikipedia.org/wiki/CLU_(programming_language)