www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile Time Execution of Inout Parameters

reply Xinok <xnknet gmail.com> writes:
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.
Mar 04 2007
next sibling parent reply Walter Bright <newshound digitalmars.com> writes:
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.
Mar 04 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
Mar 04 2007
parent reply Walter Bright <newshound digitalmars.com> writes:
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
Mar 04 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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...
Mar 04 2007
prev sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Mar 04 2007
prev sibling parent Xinok <xnknet gmail.com> writes:
 The out and inout are only for functions called by compile time 

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.
Mar 04 2007