Weird template error
Nick Sabalausky
a at a.a
Tue Nov 25 19:44:22 PST 2008
"Jarrett Billingsley" <jarrett.billingsley at gmail.com> wrote in message
news:mailman.19.1227139335.22690.digitalmars-d at puremagic.com...
> On Wed, Nov 19, 2008 at 4:59 PM, Brian <digitalmars at brianguertin.com>
> wrote:
>> I don't understand why we wouldn't want properties either. Another issue
>> I've had a couple times, although there might be a good reason for this
>> I'm not aware of:
>>
>> class Foo {
>> int x;
>> }
>>
>> void set(inout int x) {
>> x = 10;
>> }
>>
>> void main() {
>> auto foo = new Foo();
>> set(foo.x); // This works fine
>> }
>>
>> But then if you need to make x a property:
>>
>> class Foo {
>> int _x;
>>
>> int x() { return _x; }
>> int x(int val) { return _x = val; }
>> }
>>
>> You get "Error: foo.x() is not an lvalue"
>
> That one's trickier, even if you did have properties. Even "true"
> properties would still boil down to a function call. Set expects a
> reference to an integer. How do you convert getter/setter functions
> into an integer?
>
class Foo
{
// This syntax is a modified version of C#'s properties
int x
{
get { return x.rawValue; };
set { x.rawValue = newValue; }
}
}
void makeTen(inout int x)
{ x = 10; }
void main()
{
int i;
auto foo = new Foo();
makeTen(i); // Ok
// compiler knows that foo.x is an int property
// and turns this:
makeTen(foo.x);
// into this:
auto _foo_x = foo.x;
makeTen(_foo_x);
foo.x = _foo_x;
// and finally this:
auto _foo_x = foo.x.get();
makeTen(_foo_x);
foo.x.set(_foo_x);
}
> It doesn't work now because foo.x is a function, not an int.
>
> Also, I wonder if ref returns could help here..
More information about the Digitalmars-d
mailing list