It looks like in the absence of an assignment of the output of g() to some value (say int b = g()) which the compiler would insert conversion code for, the ref is retaining some pointer semantics.  I'm guessing that because writeln is variadic the compiler doesn't do anything with g's output (like dereference it, convert it to an int, etc.), so you just keep getting the pointer of 'a' back.  I don't know the internals, but I'm guessing ref is syntactic sugar to make pointer operations appear like normal value operations.  If you replaced (conceptually) ref int g() with int* g(), and replace return ++a with ++a; return &a, then you'd get the output you see.<div>
<br></div><div>- Cliff<br><br><div class="gmail_quote">On Sat, Apr 9, 2011 at 9:59 AM, Timon Gehr <span dir="ltr"><<a href="mailto:timon.gehr@gmx.ch">timon.gehr@gmx.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Whats the output of the following code supposed to be?<br>
<br>
import std.stdio;<br>
<br>
int a=0;<br>
ref int g(){<br>
    writeln("called g");<br>
    return ++a;<br>
}<br>
<br>
void main(){<br>
    int function() f=&g;<br>
    writeln(cast(int)&a);<br>
    writeln(f());<br>
    writeln(f());<br>
    writeln(f());<br>
}<br>
<br>
The output using dmd 2.052<br>
-144918776<br>
called g<br>
-144918776<br>
called g<br>
-144918776<br>
called g<br>
-144918776<br>
<br>
This is certainly wrong, as it includes an IMPLICIT cast from int* to int, but<br>
there are happening other strange things.<br>
<br>
How to distinguish taking the address of a call to a parameterless ref<br>
function from taking the address of the function itself anyways?<br>
<br>
<br>
<br>
</blockquote></div><br></div>