Thread overview
Re: Anyone know why this CTFE isn't working?
Jul 16, 2010
Rory McGuire
Jul 16, 2010
Jonathan M Davis
Jul 16, 2010
Rory McGuire
July 16, 2010
Sorry about the html

On Fri, 16 Jul 2010 11:46:48 +0200, Rory McGuire <rmcguire@neonova.co.za> wrote:
>
> import std.stdio;
>
> struct State {
> 	string s; string getString() { return s; }
> 	static State opCall(string s) {
> 		State ret;
> 		ret.s = s;
> 		return ret;
> 	}
> }
>
> void main() {
> 	auto s = State("adf");
> 	pragma(msg, s.getString());
> }
>
> dmd Output: (line 14 is the pragma statement)
>
> struct.d(14): Error: variable s cannot be read at compile time
> struct.d(14): Error: cannot evaluate s.getString() at compile time
> s.getString()
July 16, 2010
On Friday 16 July 2010 02:46:48 Rory McGuire wrote:
> import std.stdio;
> 
> struct State {
>   string s; string getString() { return s; }
>   static State opCall(string s) {
>   State ret;
>   ret.s = s;
>   return ret;
>   }
> }
> 
> void main() {
>   auto s = State("adf");
>   pragma(msg, s.getString());
> }

Make s an enum and it'll work. As it is, it's a local variable created at runtime rather than a constant at compile-time. So, use

enum s = State("adf");


- Jonathan M Davis
July 16, 2010
On Fri, 16 Jul 2010 12:05:02 +0200, Jonathan M Davis <jmdavisprog@gmail.com> wrote:

> On Friday 16 July 2010 02:46:48 Rory McGuire wrote:
>> import std.stdio;
>>
>> struct State {
>>   string s; string getString() { return s; }
>>   static State opCall(string s) {
>>   State ret;
>>   ret.s = s;
>>   return ret;
>>   }
>> }
>>
>> void main() {
>>   auto s = State("adf");
>>   pragma(msg, s.getString());
>> }
>
> Make s an enum and it'll work. As it is, it's a local variable created at
> runtime rather than a constant at compile-time. So, use
>
> enum s = State("adf");
>
>
> - Jonathan M Davis

Thanks

worked