By reading the code "foo + bar" you can't know what is really doing internally.
He is talking about operator (+-*=[]&) overloading. Not method overloading.
By reading the code "foo + bar" you can't know what is really doing internally.
He is talking about operator (+-*=[]&) overloading. Not method overloading.
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. ;)
Did you have a look at the Eigen library (C++)?
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...
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.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
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. :)
For example, in Javascript: "Hello" + " " + "World!". What the operator there is doing is concatenating the strings, so if you had a method to do it you wouldn't call it add - you'd call it concat.
In ruby you can implement certain numerical methods including +
In smalltalk + is a binary method, you can give your methods all sort of symbol names. Same with Scala I think.
Hmm, are we talking about (user defined) operator overloading as a language feature, or about overloaded operators? For example, I hate that 1/2 and 1.0/2 are different things in most languages, but I haven't heard anyone call this operator overloading in the context of C.