www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is not an lvalue

reply Caligo <iteronvexor gmail.com> writes:
With the latest DMD I've noticed that the following no longer works:

struct A { int[4] _data; }

struct B {
  ref B foo(const ref A a) { }
}

B b = B().foo(A([1, 2, 3, 4]));  // Error: A() is not an lvalue


How much of a difference is 'ref' supposed to make once DMD improves
in the future?  Am I better off not using 'ref'?  I suppose I still
don't know when to go with 'ref' and when not to.

Is there supposed to be a performance difference between something like this:

// _1
struct B {
  ref B foo(const ref A a){ }
}

A a = A([1, 2, 3, 4]);
B b = B().foo(a);

and this:

// _2
struct B {
  ref B foo(A a){ }
}

B b = B().foo(A([1, 2, 3, 4]));


???

P.S.
In the past I've noticed improvement in performance when using 'ref'.
With the latest DMD, using 'ref' is actually slightly slower (at least
in the above example)
Mar 11 2012
parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 12-03-2012 01:55, Caligo wrote:
 With the latest DMD I've noticed that the following no longer works:

 struct A { int[4] _data; }

 struct B {
    ref B foo(const ref A a) { }
 }

 B b = B().foo(A([1, 2, 3, 4]));  // Error: A() is not an lvalue


 How much of a difference is 'ref' supposed to make once DMD improves
 in the future?  Am I better off not using 'ref'?  I suppose I still
 don't know when to go with 'ref' and when not to.

 Is there supposed to be a performance difference between something like this:

 // _1
 struct B {
    ref B foo(const ref A a){ }
 }

 A a = A([1, 2, 3, 4]);
 B b = B().foo(a);

 and this:

 // _2
 struct B {
    ref B foo(A a){ }
 }

 B b = B().foo(A([1, 2, 3, 4]));


 ???

 P.S.
 In the past I've noticed improvement in performance when using 'ref'.
 With the latest DMD, using 'ref' is actually slightly slower (at least
 in the above example)

OK, so the new behavior is correct. You can only pass a variable or a field to a ref parameter, not the result of a computation of any kind (hence the error). Also, ref vs non-ref only matters once you get beyond structs of size 32 bytes in my experience. -- - Alex
Mar 11 2012