> In fact it's also possible to know that these don't return a reference to their parameter.<div><br></div><div>Watch out for this:</div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Struct S {double x;}</span></div>
<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">ref double foo(ref S a){return a.x;}</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
</div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div><font color="#222222" face="arial, sans-serif">This sounds hacky. I've proposed a general solution here: <a href="http://wiki.dlang.org/DIP38">http://wiki.dlang.org/DIP38</a> </font></div>
<div><font color="#222222" face="arial, sans-serif">either with user annotations of ref-return functions (scheme A) (just distinguishing ref vs scope ref), or with compiler taking care of annotations (scheme B).</font></div>
<div><br><div class="gmail_quote">On Sun, May 26, 2013 at 1:21 PM, Martin Nowak <span dir="ltr"><<a href="mailto:code@dawg.eu" target="_blank">code@dawg.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 05/05/2013 12:30 AM, Walter Bright wrote:<br>
> On 5/4/2013 3:03 PM, deadalnix wrote:<br>
>>> Where you miss the point, is that these annotations may be omitted<br>
>>> (and they<br>
>>> are most of the time). When nothing is specified, the lifetime of the<br>
>>> returned<br>
>>> reference is considered to be the union of the lifetime of parameters<br>
>>> lifetime, which is what you want in 99% of cases.<br>
>><br>
>> Note : We may also choose the lack of explicit lifetime means runtime<br>
>> check as<br>
>> proposed, instead of being an error.<br>
><br>
> D omits the check when it can prove that the returned ref is not a ref<br>
> to one of the parameters that is local.<br>
<br></div>
ref int foo(ref int a, ref int b);<br>
<br>
It's a very nice observation that calling foo with only non-local references means that the returned reference is non-local too.<br>
In a way this works like inout but with a safe default so<br>
that no annotation is needed.<br>
<br>
In fact it's also possible to know that these don't return a reference to their parameter.<br>
<br>
ref double foo(ref int a);<br>
<br>
Struct S {}<br>
ref double foo(ref S a);<br>
<br>
It can become somewhat complicated to check though.<br>
<br>
Anyhow I think using flow-analysis to omit runtime checks is a nice approach.<br>
</blockquote></div><br></div>