digitalmars.D.learn - Auto ref function : How is this possible ?
- matovitch (24/24) Apr 11 2015 Hi,
- matovitch (1/1) Apr 11 2015 (you can remove the ref stuff)
- matovitch (3/3) Apr 11 2015 Ok this explain it :
- matovitch (3/3) Apr 11 2015 In fact I am now thinking it's great...I tried with string
- Steven Schveighoffer (20/44) Apr 13 2015 D has great compile-time tools to examine what the compiler is doing.
- matovitch (1/1) Apr 13 2015 Thanks for the tip ! I was looking at something like this.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/7) Apr 15 2015 pragma(msg) has been added to "Programming in D" but it is not available...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/6) Apr 15 2015 Sorry for the spam :( but apparently it is already online:
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
Ok this explain it : http://dlang.org/function.html#auto-functions. It should return a float.
Apr 11 2015
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
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
Thanks for the tip ! I was looking at something like this.
Apr 13 2015
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
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