Auto ref function : How is this possible ?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 13 07:30:29 PDT 2015
On 4/11/15 6:08 AM, matovitch wrote:
> Hi,
>
> I just learn about auto ref functions and tried this :
>
> import std.stdio;
>
> auto ref foo(int i, ref float f)
> {
> if (i < f)
> {
> return i;
> }
> else
> {
> return f;
> }
> }
>
> void main()
> {
> int i = 1;
> float f1 = 1.1;
> float f2 = 0.9;
> writeln(foo(i, f1));
> writeln(foo(i, f2));
> }
>
> Tricky questions : Does it compiles ? If yes what does it do ?
> Then my question : How is this possible ?
D has great compile-time tools to examine what the compiler is doing.
A great feature of D for investigating compiler internals is pragma(msg,
...). This prints at compile time some message (a string) that is based
on the state at the time. For example:
void main()
{
int i = 1;
float f1 = 1.1;
float f2 = 0.9;
pragma(msg, typeof(foo(i, f1)).stringof); // prints what type
foo returns
auto x = foo(i, f2);
pragma(msg, typeof(x).stringof); // same thing, but easier to
understand.
}
result (prints while compiling):
float
float
-Steve
More information about the Digitalmars-d-learn
mailing list