<div class="gmail_quote">On 12 May 2012 12:37, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Saturday, May 12, 2012 11:26:37 Timon Gehr wrote:<br>
> On 05/12/2012 10:13 AM, Manu wrote:<br>
> > On 11 May 2012 21:28, Mehrdad <<a href="mailto:wfunction@hotmail.com">wfunction@hotmail.com</a><br>
> ><br>
> > <mailto:<a href="mailto:wfunction@hotmail.com">wfunction@hotmail.com</a>>> wrote:<br>
> >     Yes, I agree, but consider that D users should NOT have to work with<br>
> >     pointers to do something so basic<br>
> ><br>
> > I'd like to think this were true, but the fact that 'ref' barely works<br>
> > makes this almost immediately false when trying to write any non-trivial<br>
> > program.<br>
><br>
> It depends on the coding style.<br>
<br>
</div></div>I rarely end up passing by either ref or pointer, and the only issues that<br>
I've ever had with ref relate to the fact that you can't pass it rvalues.<br>
Obviously both pointers and ref have their uses, but I'd definitely have to<br>
concur that it depends on your coding style and what you're doing. Also, I<br>
really don't know what's wrong with ref such that anyone could say that it<br>
"barely works" other than the issues with rvalues and functions which take ref<br>
or const ref.<br></blockquote><div><br></div><div><div>struct X</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>int i;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">      </span>X opBinary(string op)(const ref X b) if(op == "+") { return X(i + b.i); }</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>ref X opOpAssign(string op)(const ref X b) if(op == "+") { i += b.i; return this; }</div><div><div><span class="Apple-tab-span" style="white-space:pre">     </span>...etc</div>
</div><div>}</div><div><br></div><div>ref X func(ref X x)</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>return x;</div><div>}</div><div><br></div><div>bool maybe()</div><div>{</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>return (time() & 1) != 0;</div><div>}</div><div><br></div><div>void test()</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>X a,b,c;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>X v[];</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">      </span>func(a); // this is basically the only expression that works</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>X t = a + (b + c); // fail</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>a += b + c; // fail</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>ref X t = func(a); // fail</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>func(a + b); // fail</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>func(maybe() ? a : b); // this generates bad code: <a href="http://forum.dlang.org/thread/cnwpmhihmckpjhlaszzy@forum.dlang.org">http://forum.dlang.org/thread/cnwpmhihmckpjhlaszzy@forum.dlang.org</a></div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>ref X t = v[some + complex * (expression + that_i_dont_want_to_repeat())]; // fail</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">       </span>foreach(i; 0..10)</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>ref X t = v[some + expression * involving - i]; // fail</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>}</div>
</div><div>}</div></div><div><br></div><div>Just a couple of extremely common usage patterns that come to mind. I'm sure there are many more...</div></div>