Properties: a.b.c = 3
Nick Sabalausky
a at a.a
Thu Jul 30 15:04:19 PDT 2009
"Ary Borenszweig" <ary at esperanto.org.ar> wrote in message
news:h4su5p$2ctj$1 at digitalmars.com...
> Nick Sabalausky wrote:
>>
>> C# shows us *A* solution. Doesn't mean we can't do one better.
>>
>> The whole point of properties is that, to the outside observer, they
>> behave, as much as possible, like plain fields. Obviously this can't be
>> done in all cases (like getting an int* from &myIntProp), but in this
>> case:
>>
>> widget.sizeAsAStructField.width = 10; // Works
>> widget.sizeAsAStructProp.width = 10; // Doesn't work
>>
>> ...we absolutely can fix that even if C# doesn't.
>
> I don't understand your example. What's wrong with it? What doesn't work
> as you expect and what is your expected result?
>
> In the first case, if you modify sizeAsAStructField.width, it's ok and
> should compile, because you are modifying sizeAsAStructField itself, not a
> copy of it. In C#:
>
Here's the problem (still in C#):
---------------------
public struct MyStruct
{
public int x;
}
public class ClassWithProp
{
// The whole point of properties is that the interface
// for these two should work as similarly as possible.
public MyStruct field;
public MyStruct prop {get;set;}
}
static Main()
{
ClassWithProp classWithProp = new ClassWithProp();
// Works
classWithProp.field.x = 10;
// Doesn't currently work in either C# or D.
// *But*, unlike "&field" vs "&prop", this prop-vs-field
// inconsistency *is* easily fixable,
// and therefore should be fixed.
classWithProp.prop.x = 10;
}
---------------------
More information about the Digitalmars-d
mailing list