<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 25-mar-10, at 00:09, Fawzi Mohamed wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><br>On 24-mar-10, at 23:29, Andrei Alexandrescu wrote:<br><br><blockquote type="cite">[...]</blockquote><blockquote type="cite">So arrays have a different interface than streams. It looks like you can't write code that works uniformly for both, because for some you need the * and for some you don't. Did I understand that correctly?<br></blockquote><br>well the foreach loop is the same, but the iteration loop is indeed different in the sense that one uses a pointer to an element and the other the element itself.<br>one can write code that removes the pointer that is there (dereferencing it, or doing and inline function with subsequent call which allows you to reuse the same variable name):<br>void myF(ref x){<br> // code<br>}<br>myF(*x);<br><br>(that is a nice trick that I used several times).<br><br>But yes there *is* a difference and the difference is that with arrays you might modify the element, modifying the stored value, whereas with streams you can't.<br>This conceptual difference and if reflected in the interface.<br>One can then discuss if immutable arrays should be iterated with immutable pointers or with values (i.e. copying) just as streams are.<font class="Apple-style-span" color="#000000"><font class="Apple-style-span" color="#144FAE"><br></font></font></div></blockquote><br></div><div>thinking more about this, you are right something that returns a ref can be used exactly the same way as something that returns a value if one takes the value with</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>auto val=returnRefOrVal;</div><div>can be used as value exactly in the same way.</div><div>Whereas something that returns a T or a T* , need an explicit conversion.</div><div>That is easy to do, and one can even easily wrap the delegate in place with something that returns T instead of T*, but the conversion has to be explicit (before feeding it to the code), or explicitly tested for in the code.</div><div>In practice I hadn't real problems due to this, but it is something that is uglier than ref return.</div><div>On the other hand it is easier to know if you might modify the value that you received expecting to modify the underlying structure.</div><div><br></div></body></html>