9 ms·
How did you solve the problem in your matrix libraries of overloading a single operator multiple times? I was trying to make rudimentary, game-oriented linear a
by bjz 14y ago
How did you solve the problem in your matrix libraries of overloading a single operator multiple times? I was trying to make rudimentary, game-oriented linear algebra library in rust along the lines of glm. I immediately ran into the problem of not being able to implement "mat4 * float->mat4", "mat4 * vec4->vec4" and "mat4 * mat4->mat4" overloads at once. The alternative was only to go with "mult_float", "mult_vec4" and "mult_mat4", but as you say this leads to "nigh-unreadable" code.
It might seem petty, but whilst I understand rust avoiding overloading functions entirely (due to type problems and code obfuscation), it was enough to turn me off entirely, in this case sending back to D.
Bear in mind this is not to say Rust is a bad language - there's plenty to like about it. ;)
- LinaLauneBaer 14y agoI don't know rust but rust seems very much like C++. Did you have a look at the Eigen library (C++)? http://eigen.tuxfamily.org/dox/TutorialMatrixClass.html http://eigen.tuxfamily.org/dox/TutorialMatrixClass.html I have never worked with this library but it seems to me that they have not the problems you described. Maybe having a look at it brings up some new ideas...
- bjz 14y agoAlthough on superficial level it shares the AGOL/C-style syntax, Rust is a very different language from C++. Some things are possible or easier in Rust as opposed to C++ and vice-versa. Copying directly from a C++ library would be difficult, and wouldn't take advantage of Rust's unique strengths.
- LinaLauneBaer 14y agoOkay. Thanks for the info. I just had a quick look at Rusts wikipedia entry and saw that it has been influenced by C++. But as you pointed out, this might not mean much...
- pcwalton 14y agoI was about to say "you can't do it", but I think you can -- the trick is to use a bounded generic implementation. Once "Mul" becomes a trait, you'll be able to say this: trait MatrixMultiplyRHS<Result> { fn mul(matrix: Matrix) -> Result; } impl<RHS:MatrixMultiplyRHS<Result>,Result> Matrix : Mul<RHS, Result> { fn mul(rhs: RHS) -> Result { rhs.mul(self) } } impl float : MatrixMultiplyRHS<Matrix> { fn mul(matrix: Matrix) -> Matrix { // ...implementation of matrix scalar multiply... } } impl Vector : MatrixMultiplyRHS<Vector> { fn mul(matrix: Matrix) -> Vector { // ... implementation of matrix multiply for vectors ... } } impl Matrix : MatrixMultiplyRHS<Matrix> { fn mul(matrix: Matrix) -> Matrix { // ... implementation of matrix multiply for matrices ... } } It's admittedly a bit awkward, but maybe that's OK to discourage overloading unless you actually need it. Still, your point was very interesting -- I didn't realize this was possible! -- and I'll spread it around the team.
- bjz 14y agoAhh neat. Just as a warning, I've already aired this topic on github, so maybe that might be a good place to discuss it: https://github.com/mozilla/rust/issues/2961 https://github.com/mozilla/rust/issues/2961 I don't want to cause a fuss. While Rust might not work for my needs/wants/desires, that's ok. I highly respect those who don't attempt to please everyone. :)