Anyone know why this CTFE isn't working?
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 timestruct.d(14): Error: cannot evaluate s.getString() at compile times.getString() Rory McGuire R&D Tel : +27 (033) 386 7263 Cell : +27 (082) 856 3646 Email: rmcgu...@neonova.co.za Website: www.neonova.co.za VCard: View This email and its attachments may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of NeoNova. If you are not the intended recipient of this email and its attachments, you must take no action based upon them, nor must you copy or show them to anyone. Please contact the sender if you believe you have received this email in error.
Re: Anyone know why this CTFE isn't working?
On Fri, 16 Jul 2010 11:46:48 +0200, 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()); > } > > 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() It's not working because s isn't a compile-time quantity. Try: enum s = State("adf"); -Lars
Re: Anyone know why this CTFE isn't working?
Sorry about the html On Fri, 16 Jul 2010 11:46:48 +0200, 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()); } 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()
Re: Anyone know why this CTFE isn't working?
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
Re: Anyone know why this CTFE isn't working?
On Fri, 16 Jul 2010 11:58:57 +0200, Lars T. Kyllingstad wrote: On Fri, 16 Jul 2010 11:46:48 +0200, 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()); } 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() It's not working because s isn't a compile-time quantity. Try: enum s = State("adf"); -Lars Awesome thanks, worked. So is the difference that "auto s" is a Struct which can change whereas "enum s" is a constant? If it is a constant its just "s" that is constant right? Thanks Lars -Rory
Re: Anyone know why this CTFE isn't working?
On Fri, 16 Jul 2010 12:05:02 +0200, Jonathan M Davis 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
Re: Anyone know why this CTFE isn't working?
On Fri, 16 Jul 2010 12:12:38 +0200, Rory McGuire wrote: > On Fri, 16 Jul 2010 11:58:57 +0200, Lars T. Kyllingstad > wrote: > >> On Fri, 16 Jul 2010 11:46:48 +0200, 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()); >>> } >>> >>> 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() >> >> It's not working because s isn't a compile-time quantity. Try: >> >> enum s = State("adf"); >> >> -Lars > > Awesome thanks, worked. > > So is the difference that "auto s" is a Struct which can change whereas > "enum s" is a constant? > If it is a constant its just "s" that is constant right? > > Thanks Lars Yes. Writing "auto s = State("adf");" is equivalent to writing State s = State("adf"); and since s can change at runtime, it would be meaningless to say that it has a value at compile time. However, its *initial value*, the struct literal State("adf"), is known at compile time -- otherwise it would be impossible to assign it to an enum. And actually, an enum is not only a constant, it is a manifest constant. When you declare "enum x = someFixedValue", the compiler will just replace all later uses of x with someFixedValue. It's basically the same as using the literal value in those places. This has the consequence that you can't take the address of an enum, nor pass an enum by reference. Doing enum i = 3; int* p = &i; is equivalent to int* p = &3; which doesn't make sense. -Lars