6 ms·
Try this on: plus( mul(vec1, vec2), mul(vec3, vec4)) Yeah it's a tiny bit clumsier, and prefix notation takes some getting used to. But on
by coldpie 3y ago
Try this on:
plus(
mul(vec1, vec2),
mul(vec3, vec4))
Yeah it's a tiny bit clumsier, and prefix notation takes some getting used to. But on the plus side we avoid all the too-clever travesties programmers have inflicted on us with bad operator overloading decisions! On the whole I think it's easily worth the trade.
- ThemalSpan 3y agoSorry but please don't take Eigen (https://eigen.tuxfamily.org https://eigen.tuxfamily.org) away from me. Can't speak for others, but the scientific code I work on would become unreadable like that.
- sva_ 3y agoThe site seems to be down https://downforeveryoneorjustme.com/eigen.tuxfamily.org?proto=https https://downforeveryoneorjustme.com/eigen.tuxfamily.org?prot...
- spacechild1 3y agoAgain, no thanks. I want mathematical notation and I simply won't use any language without operator overloading. Free functions for common mathematical operations are an abomination.
- throwaway894345 3y agoThen you should probably use a language that lets you write DSLs for any given domain, rather than abusing operator overloading which just happens to work for a few subdomains of mathematics (e.g., you can't use mathematical conventions for dot product multiplication in C++). Anyway, I've never seen any bugs because someone misunderstood what a `mul()` function does, but I've definitely seen bugs because they didn't know that an operator was overloaded (spooky action at a distance vibes).
- pjmlp 3y agoAssuming mul does actually a multiplication.
- throwaway894345 3y agoIt doesn’t matter, you can look it up easily because without overloading, you know exactly which implementation will run.
- pjmlp 3y agoAh, just like I do when I press F12 over an operator.
- enriquto 3y agoYou come across as a bit facetious here. Of course, the compiler or an advanced IDE can know what your code means. If all your identifiers were random permutations of l and I: lIllI1lI, your IDE would not mind either, but the code would be horrific, don't you agree? The point of the OP is that overloaded operators (and functions) make it harder to reason about the code for a human that reads it. At least for some people. At the end, everything is "just" syntactic sugar, but it makes a significant difference.
- throwaway894345 3y agoExactly. If you don't care that the code is unreadable and you can rely on every human viewing the code through an IDE with symbol resolution (and not say, online code review platforms) and remembering to use said symbol resolution to check every operator, then operator overloading is great!
- spacechild1 3y agoActually, I'm quite happy what C++ has to offer :) Yes, the * operator can be ambiguous in the context of classic vector math (although that is just a matter of documentation), but not so much with SIMD vectors, audio vectors, etc. Again: a) vec4 = (vec1 - vec2) * 0.5 + vec3 * 0.3; or b) vec4 = plus(mul(minus(vec1, vec2), 0.5), mul(vec3, 0.3)); Which one is more readable? That's pretty much the perfect use case for operator overloading.
- moshegramovsky 3y ago[dead]
- dekhn 3y agoplus(x, y) IS mathematical notation. It's Polish Notation without symbol shortcuts. Mathematica has used this for decades.
- danzk 3y agoIt's Polish Notation actually.
- dekhn 3y agoThanks, corrected in place.
- spacechild1 3y agosure, I was a bit sloppy there, but I think you know what I meant. Polish notation is just not how most people write arithmetic expressions :)
- kazinator 3y agomul(x, y) can be overloaded! x and y could be any cobination of matrix, vector, or integer, float. You're mixing up overloading (which is semantics) with syntax.
- spacechild1 3y agoWe all know that functions can be overloaded. The emphasis is on operator overloading. So yes, it is about syntax.
- slavik81 3y agoWhy have operators at all? If that notation is good enough, then you might as well use it for the built-in types too. We're halfway to designing a Lisp!
- pjmlp 3y agoHow can you be sure that plus() and mul() actually do what they say?
- coldpie 3y agoThey're functions. Whatever they do, my code will go execute some code from whatever library implements them, which is what a function does. I just want to be able to rely on [] being an array subscript when I read some unfamiliar code. Is that too much to ask?
- pjmlp 3y agoJust like the function that happens to be named [].
- kristiandupont 3y agoCan IDE's detect this and offer "go to implementation" on an overloaded operator these days? Because besides from the surprise-element in the fact that there even is code to debug hiding somewhere, not being able to quickly navigate to it is much worse in my opinion. And with infix operators where you can't even be sure which operand the implementation belongs to, figuring it out can be a bit of a detective task.
- pjmlp 3y agoVisual Studio does show a popup, go to implementation, and if the implementation happens to be visible on the same screen, also highlights it.
- circuit10 3y agoIf you’re using it right then [] is still an array subscript, just with a custom implementation for getting array elements
- throwaway894345 3y agoYou read the code. And unlike operator overloading, you know at a glance exactly which implementation to look at. There is no spooky action at a distance.
- bigbillheck 3y agoFunction overloading's not that much better.
- dleslie 3y agoEven better: (plus (mul vec1 vec2) (mul vec3 vec4)) Lisp is love.