www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Auto ref function : How is this possible ?

reply "matovitch" <camille.brugel laposte.net> writes:
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 ?
Apr 11 2015
next sibling parent reply "matovitch" <camille.brugel laposte.net> writes:
(you can remove the ref stuff)
Apr 11 2015
parent reply "matovitch" <camille.brugel laposte.net> writes:
Ok this explain it : 
http://dlang.org/function.html#auto-functions. It should return a 
float.
Apr 11 2015
parent "matovitch" <camille.brugel laposte.net> writes:
In fact I am now thinking it's great...I tried with string 
instead of float and got a clear error message. I should have 
read the spec more thoroughly.
Apr 11 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Apr 13 2015
parent reply "matovitch" <camille.brugel laposte.net> writes:
Thanks for the tip ! I was looking at something like this.
Apr 13 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/13/2015 07:44 AM, matovitch wrote:
 Thanks for the tip ! I was looking at something like this.
pragma(msg) has been added to "Programming in D" but it is not available online yet: https://bitbucket.org/acehreli/ddili/src/ae49747a850fabc3a3e66dcdb3626a991c53632e/src/ders/d.en/templates.d?at=master#cl-696 Ali
Apr 15 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/15/2015 10:50 AM, Ali Çehreli wrote:

 pragma(msg) has been added to "Programming in D" but it is not available
 online yet:
Sorry for the spam :( but apparently it is already online: http://ddili.org/ders/d.en/templates.html#ix_templates.pragma Ali
Apr 15 2015