Property rewriting; I feel it's important. Is there still time?

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 11 12:37:59 PST 2010


On Wed, 10 Mar 2010 17:16:11 -0500, Pelle Månsson  
<pelle.mansson at gmail.com> wrote:

> On 03/10/2010 10:14 PM, Steven Schveighoffer wrote:
>> I think this is fine as long as we don't take it to the extreme. That
>> is, I don't want to see this happening:
>>
>> foo.prop1.prop2++;
>>
>> is rewritten to
>>
>> auto p1 = foo.prop1;
>> auto p2 = p1.prop2;
>> p2++;
>> p1.prop2 = p2;
>> foo.prop1 = p1;
>>
>> I think one level of lowering is enough to handle the most common cases.
>>
>> Of course, if a property returns an lvalue, then it should just work.
>>
>> -Steve
>
> Why would you not want that? That's exactly what should happen! Why not?  
> I'm sorry if I'm missing something obvious.

BTW, C# doesn't do this:


struct C
{
     private int _x;
     public int x
     {
         get
         {
             return _x;
         }
         set
         {
             _x = value;
         }
     }
}

struct D
{
     private C _c;
     public C c
     {
         get
         {
             return _c;
         }
         set
         {
             _c = value;
         }
     }
}

class X
{
     static void Main()
     {
         D d = new D();
         d.c.x += 5;
     }
}


testme.cs(38,11): error CS1612: Cannot modify a value type return value of  
`D.c'. Consider storing the value in a temporary variable
testme.cs(1,8): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

-Steve



More information about the Digitalmars-d mailing list