Feature request: Make object composition easier

TommiT tommitissari at hotmail.com
Mon Mar 25 11:15:20 PDT 2013


Given the following:

struct Foo
{
     int val;
     ref Foo opUnary(string op : "++")()
     {
         ++val;
         return this;
     }
}

struct Bar
{
     Foo _foo;
     alias _foo this;

     void fun() { }
}

Bar bar;

... I would like to be able to say:

(++bar).fun();

... which would be lowered to:

(*cast(Bar*) ((cast(byte*) &++bar) + 
Bar.init._foo.offsetof)).fun();

... except that it would be considered @safe.

The logic is that certain operators, like pre-increment and 
assignment operators and such, can be assumed to be return a 
reference to the operand variable (given that the operator 
returns a ref to that type). Let's call this particular set of 
operators X. The rule would then be: If a call to an operator is 
re-routed through alias-this to a field of the operand, and the 
operator is in X, and the operator returns a ref to the type of 
the field, then the return value is implicitly converted to a ref 
to the operand.


More information about the Digitalmars-d mailing list