Suggestion: wrapping up value type objects
Kristian
kjkilpi at gmail.com
Wed Oct 25 07:17:13 PDT 2006
On Wed, 25 Oct 2006 15:24:07 +0300, Don Clugston <dac at nospam.com.au> wrote:
> Kristian wrote:
[snip]
>> So, why don't add the 'opClone' operator to the language? I mean, there
>> already are 'opXxx' functions. Why don't standardize the way of
>> creating copies/clones?
>> Then you could get rid of all the 'opXxx' and 'opXxx_r' functions:
>> they can be expressed by using 'opClone' and 'opXxxAssign' only.
>
> Not necessarily. Consider Vector and Matrix.
>
> Matrix a;
> Matrix b;
> Vector c;
> a = b * c;
> a = b.opClone().opMulAssign(c); // OK.
>
> But
> a = c * b;
> a = c.opClone().opMulAssign(b);
> // Oops -- we cloned the vector, should have cloned the matrix instead.
>
> In fact, in general you cannot even assume that
> (typeof(a*b) == typeof(a)) | (typeof(a*b) == typeof(b))
>
> which means that cloning would sometimes create useless temporary
> objects.
Well, in you example (of course, I see your point though) 'Vector' should
not have the 'opMulAssign(Matrix)' function (because it makes no sense),
so "a = c * b" would then be read as:
a = b.opClone().opMulAssign(c); //ok
Lets have two classes, 'A' and 'B'.
'a' is type of 'A' and 'b' of 'B'.
Now if we have:
typeof(a * b) == typeof(a)
typeof(b * a) == typeof(b)
then 'mathematics will be broken' (or how should I put it). When this
should be possible?
In the example above "c * b" should produce a Matrix, not a Vector.
*But* if it would be ok to produce Vector instead, then
a = c.opClone().opMulAssign(b);
would also be correct (assuming that you can assign a 'Vector' in 'a'),
and no useless temporary objects are created.
And if we have:
typeof(a * b) == typeof(b * a) == typeof(a)
then only 'A' will contain the corresponding operators (i.e.
'opMulAssign(B)'). Thus there will be no problem to decide which object to
clone.
So we can use cloning, and the 'opXxx' and 'opXxx_r' functions are not
needed.
More information about the Digitalmars-d
mailing list