View mode: basic / threaded / horizontal-split · Log in · Help
March 04, 2007
Compile Time Execution of Inout Parameters
In the D 1.007 change log, it states:
out and inout parameters are now allowed for compile time function 
execution.

Does anybody know exactly what this means? I've tried a few different 
things, but no luck at getting anything to compile.
March 04, 2007
Re: Compile Time Execution of Inout Parameters
Xinok wrote:
> In the D 1.007 change log, it states:
> out and inout parameters are now allowed for compile time function 
> execution.
> 
> Does anybody know exactly what this means? I've tried a few different 
> things, but no luck at getting anything to compile.

The out and inout are only for functions called by compile time executed 
functions.
March 04, 2007
Re: Compile Time Execution of Inout Parameters
Walter Bright wrote:
> Xinok wrote:
>> In the D 1.007 change log, it states:
>> out and inout parameters are now allowed for compile time function 
>> execution.
>>
>> Does anybody know exactly what this means? I've tried a few different 
>> things, but no luck at getting anything to compile.
> 
> The out and inout are only for functions called by compile time executed 
> functions.

When Xinok first posted that, I thought he was right; I couldn't get 
such functions to compile either. After your post I tried again and it 
worked. After some investigation, I found the difference:
---
urxae@urxae:~/tmp$ cat test.d
int bar(out int x) { x = 2; return 1; }

int foo() { int y; bar(y); return y; }

const int z = foo();

void main(){}

urxae@urxae:~/tmp$ dmd test.d
gcc test.o -o test -m32 -lphobos -lpthread -lm -Xlinker 
-L/home/urxae/opt/dmd/lib
urxae@urxae:~/tmp$ nano test.d
urxae@urxae:~/tmp$ cat test.d
void bar(out int x) { x = 2; }

int foo() { int y; bar(y); return y; }

const int z = foo();

void main(){}

urxae@urxae:~/tmp$ dmd test.d
test.d(3): Error: cannot evaluate bar(y) at compile time
test.d(5): Error: cannot evaluate foo() at compile time
---

It seems functions returning void can't be executed at compile time, 
even if they have out/inout parameters...


Reported: http://d.puremagic.com/issues/show_bug.cgi?id=1021
March 04, 2007
Re: Compile Time Execution of Inout Parameters
Frits van Bommel wrote:
> It seems functions returning void can't be executed at compile time, 
> even if they have out/inout parameters...

This is by design:

1) functions with no output make no sense evaluating at compile time

2) it makes no sense to have a compile time function return its value 
via an out or inout rather than the return value
March 04, 2007
Re: Compile Time Execution of Inout Parameters
Walter Bright wrote:
> Frits van Bommel wrote:
>> It seems functions returning void can't be executed at compile time, 
>> even if they have out/inout parameters...
> 
> This is by design:
> 
> 1) functions with no output make no sense evaluating at compile time

agreed.

> 2) it makes no sense to have a compile time function return its value 
> via an out or inout rather than the return value

This just seems to be a pretty arbitrary restriction. I'm not saying I'd 
use it for anything other than testing if it works, but I don't see any 
technical reason to forbid it either...
March 04, 2007
Re: Compile Time Execution of Inout Parameters
> The out and inout are only for functions called by compile time 
executed functions.

I figured that much. Frits cleared it up though, it was because I had no 
return type that the compiler was restricting me from compiling it.

I think this is one restriction which should be lifted, or at least 
modified to allow functions with out / inout parameters. I believe 
restrictions should only be put in place in a language if it prevents 
something dangerous, such as returning the address of a local variable, 
or using an uninitalized variable. This is one restriction which doesn't 
prevent anything dangerous, and is just something which programmers will 
be forced to work around.
March 04, 2007
Re: Compile Time Execution of Inout Parameters
Walter Bright wrote:
> Frits van Bommel wrote:
>> It seems functions returning void can't be executed at compile time, 
>> even if they have out/inout parameters...
> 
> This is by design:
> 
> 1) functions with no output make no sense evaluating at compile time
> 
> 2) it makes no sense to have a compile time function return its value 
> via an out or inout rather than the return value

That seems kind of arbitrary.  The first thought that came to mind was 
what if you need to return two values?  For instance a min-max function 
that returns both the minimum and maximum value.  In that case it would 
be weird to arbitrarily pick one as the "real" return value and make the 
other an out parameter.

--bb
Top | Discussion index | About this forum | D home