O
Oscar Papel
Hello,
I have a c# class that holds a large matrix of data.
I overrode the standard +, - operators but I also need to override the +=
and -= operators in order to avoid making a copy of the entire matrix.
The code below should explain...
From what I can tell, the ECMA spec does not allow for overriding of A+=B
and expands it to A=A+B
This is a huge waste of memory for my app.
Instead, I have to force the user of the class to use A.Add(B) .
Is there anything I can do or am I stuck?
How do I go about requesting this ability for the next version of the
language?
Also, Is there a way of adding custom operators to the language or is the
list of operators set in stone?
I would also love to see an overrideable Min/Max operator.
Sometimes, Math.Min doesn't cut it.
<code>
public class Matrix {
// this is what I'd like to do...
public static void operator += (Matrix A,Matrix B) { A.Add(B); }
// but this is what I have to do
public static Matrix operator + (Matrix A, Matrix B) { return
Matrix.Add(A,B); }
public static Matrix Add (Matrix A, Matrix B) {
Matrix retval=new Matrix(A) // <--- I want to avoid having to
reallocate a new matrix
retval.Add(B);
return retval;
}
public void Add(Matrix B) { ... add code here }
}
</code>
I could change the code above to always use the A matrix but then expresions
like C=A+B would be misleading since A would also change.
Thanks in advance,
Oscar
I have a c# class that holds a large matrix of data.
I overrode the standard +, - operators but I also need to override the +=
and -= operators in order to avoid making a copy of the entire matrix.
The code below should explain...
From what I can tell, the ECMA spec does not allow for overriding of A+=B
and expands it to A=A+B
This is a huge waste of memory for my app.
Instead, I have to force the user of the class to use A.Add(B) .
Is there anything I can do or am I stuck?
How do I go about requesting this ability for the next version of the
language?
Also, Is there a way of adding custom operators to the language or is the
list of operators set in stone?
I would also love to see an overrideable Min/Max operator.
Sometimes, Math.Min doesn't cut it.
<code>
public class Matrix {
// this is what I'd like to do...
public static void operator += (Matrix A,Matrix B) { A.Add(B); }
// but this is what I have to do
public static Matrix operator + (Matrix A, Matrix B) { return
Matrix.Add(A,B); }
public static Matrix Add (Matrix A, Matrix B) {
Matrix retval=new Matrix(A) // <--- I want to avoid having to
reallocate a new matrix
retval.Add(B);
return retval;
}
public void Add(Matrix B) { ... add code here }
}
</code>
I could change the code above to always use the A matrix but then expresions
like C=A+B would be misleading since A would also change.
Thanks in advance,
Oscar