www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - with() statement doesn't want to work with property functions

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
struct Bar { int x; }
struct Foo
{
    Bar _bar;
    Bar bar()
    {
        return _bar;
    }
}

void main()
{
    Foo foo;
    with (foo.bar)
    {
    }
}

Error: foo.bar() is not an lvalue

I've made a getter because I want to control how _bar is manipulated.
I've lost the ability to use the with statement, which is a minor
inconvenience. Is there a specific reason why with() should not be
allowed to be used with property functions?

This works fine:

immutable Foo foo;
with (foo)
{
}

So I don't see why it shouldn't work on property functions?
Sep 18 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/18/2011 05:14 PM, Andrej Mitrovic wrote:
 struct Bar { int x; }
 struct Foo
 {
      Bar _bar;
      Bar bar()
      {
          return _bar;
      }
 }

 void main()
 {
      Foo foo;
      with (foo.bar)
      {
      }
 }

 Error: foo.bar() is not an lvalue

 I've made a getter because I want to control how _bar is manipulated.
 I've lost the ability to use the with statement, which is a minor
 inconvenience. Is there a specific reason why with() should not be
 allowed to be used with property functions?

 This works fine:

 immutable Foo foo;
 with (foo)
 {
 }

 So I don't see why it shouldn't work on property functions?

It is that way because the implementation of with is still a quick hack. I think it is implemented like this: 1. If the scope does not require a 'this' pointer, just do lookup differently. 2. If the scope does require a 'this' pointer, declare a hidden pointer to the value of the expression, and redirect all lookups that must be performed on the expression to that pointer. Obviously, if the address of the expression cannot be taken, the compiler should actually store the expression in a hidden stack variable. I think this is worth a bug report if it is not known already.
Sep 18 2011