Thread overview
enum return type
Mar 05, 2014
Frustrated
Mar 05, 2014
bearophile
Mar 05, 2014
Frustrated
Mar 05, 2014
Adam D. Ruppe
Mar 05, 2014
Frustrated
Mar 05, 2014
Adam D. Ruppe
Mar 06, 2014
John Colvin
March 05, 2014
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?
March 05, 2014
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
March 05, 2014
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.
March 05, 2014
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.
March 05, 2014
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.
March 05, 2014
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.
March 06, 2014
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.