Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
April 11, 2015 Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
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 ? |
April 11, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | (you can remove the ref stuff) |
April 11, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | Ok this explain it : http://dlang.org/function.html#auto-functions. It should return a float. |
April 11, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | 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. |
April 13, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | 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
|
April 13, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Thanks for the tip ! I was looking at something like this. |
April 15, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | 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 |
April 15, 2015 Re: Auto ref function : How is this possible ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
Copyright © 1999-2021 by the D Language Foundation