www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum return type

reply "Frustrated" <Frustrated nowhere.com> writes:
how does an enum return type work?

enum foo(string s) { return s; }

is it a purely compile time construct?

That is, we can guarantee that foo, as a function, won't exist at 
runtime? e.g., it is a true ctfe instead of a function that can 
be executed at compile time or runtime?
Mar 05 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Frustrated:

 how does an enum return type work?

 enum foo(string s) { return s; }
As far as I know that's not valid D (but D is extremely tolerating). Bye, bearophile
Mar 05 2014
parent "Frustrated" <Frustrated nowhere.com> writes:
On Wednesday, 5 March 2014 at 23:20:08 UTC, bearophile wrote:
 Frustrated:

 how does an enum return type work?

 enum foo(string s) { return s; }
As far as I know that's not valid D (but D is extremely tolerating). Bye, bearophile
Well, it works... not sure exactly what it's doing though.
Mar 05 2014
prev sibling next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 5 March 2014 at 23:17:45 UTC, Frustrated wrote:
 is it a purely compile time construct?
I think it is the same as auto return functions. Both auto and enum in this context are storage classes. In the compiler, it looks like enum in this context forwards to parse declaration, just like keywords such as pure, which can then find it is a function. If I'm reading this correctly, it does set the manifest constant flag, but otherwise just ignores it and indeed treats it the same as an auto return value. So nothing special, arguably just the parser not throwing an error when it perhaps could.
Mar 05 2014
parent reply "Frustrated" <Frustrated nowhere.com> writes:
On Wednesday, 5 March 2014 at 23:36:34 UTC, Adam D. Ruppe wrote:
 On Wednesday, 5 March 2014 at 23:17:45 UTC, Frustrated wrote:
 is it a purely compile time construct?
I think it is the same as auto return functions. Both auto and enum in this context are storage classes. In the compiler, it looks like enum in this context forwards to parse declaration, just like keywords such as pure, which can then find it is a function. If I'm reading this correctly, it does set the manifest constant flag, but otherwise just ignores it and indeed treats it the same as an auto return value. So nothing special, arguably just the parser not throwing an error when it perhaps could.
It would be cool if it was a compile time value though. That way you could make sure code generating ctfe's were never included in the binary.
Mar 05 2014
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 5 March 2014 at 23:41:30 UTC, Frustrated wrote:
 It would be cool if it was a compile time value though. That 
 way you could make sure code generating ctfe's were never 
 included in the binary.
Yeah... one way to kinda do that now is to write it all inside a if(__ctfe) {} block. string ctfe() { if(__ctfe) { implementation } else assert(0); } Still something in the binary but not the whole thing since if(__ctfe) in codegen is changed to if(0) and removed as obviously dead code.
Mar 05 2014
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 5 March 2014 at 23:17:45 UTC, Frustrated wrote:
 how does an enum return type work?

 enum foo(string s) { return s; }

 is it a purely compile time construct?

 That is, we can guarantee that foo, as a function, won't exist 
 at runtime? e.g., it is a true ctfe instead of a function that 
 can be executed at compile time or runtime?
It's not currently possible to force something out of the binary, but to guarantee that something isn't called at runtime you can always just add an assert(__ctfe); at the start of the function or in the "in" contract.
Mar 06 2014