Suggestion: wrapping up value type objects

Don Clugston dac at nospam.com.au
Wed Oct 25 08:15:09 PDT 2006


Kristian wrote:
> 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

But how does the compiler know that this is a legal transformation?

> 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?

You've misunderstood me. My point was that it might be a third type. 
There's no guarantee that an opXXX function returns the same type as 
either of its operands.

One can define for example Vector * TransposeVector = Matrix.

opAdd() is a more general operation than opAddAssign(); it can't always 
be reduced to opClone() and opAddAssign().



More information about the Digitalmars-d mailing list