Suggestion: wrapping up value type objects

Kristian kjkilpi at gmail.com
Thu Oct 26 05:09:51 PDT 2006


On Wed, 25 Oct 2006 20:01:50 +0300, Kristian <kjkilpi at gmail.com> wrote:
[snip]
> Now, if one tries to write an operator function 'A.opXyz(B)' that  
> returns 'A' or 'B', the compiler will throw an error saying "define  
> 'A.opXyzAssign(B)' or 'B.opXyzAssign(A)' function instead".

Of course, the error message is not wanted when you cannot access the  
source code of a class.
For example:

     class A;  //imported from a library

     class B {
         A opAdd(B);  //should be allowed
     }

Even if "A opAdd(B)" logically should be logically defined in 'A' (as "A  
opAddAssign(B)"), one cannot do it because the source code of 'A' is  
inaccessible.

One solution could be using a new keyword. For example:

     class B {
         extend A opAdd(B);
     }

Ok, 'extend' isn't probably a good one, I just quickly invented it because  
"A opAdd(B);" should be in 'A' so it kind of extends 'A', and "extend A  
opAdd(B);" can be read as "extend A" +  "opAdd(B);".


Ok, that would allow one to write:

     A a;
     B b;

     a = a + b;

But not:

     a += b;

So, the following could be possible instead:

     class B {
         extend A opAddAssign(B);
     }

Now it really extends 'A' because 'B' should/cannot have such an operator  
(it returns 'A' instead of 'B'). This would allow both the cases (with the  
help of 'opClone' as described earlier):

     a = a + b;
     a += b;

You can think of "A opAddAssign(B);" actually being defined in 'A' instead  
of 'B', whenever 'B' is used with 'A' that is.

(I am assuming here that 'B' can access all the necessary methods of 'A'  
to create such operators.)



More information about the Digitalmars-d mailing list