www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Anyone know why this CTFE isn't working?

reply "Rory McGuire" <rmcguire neonova.co.za> writes:
Content-Type: multipart/alternative; boundary=----------9lCOoKqZccsfJhwva5P5Zx

------------9lCOoKqZccsfJhwva5P5Zx
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit



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()

www.neonova.co.za: http://cf.neonova.co.za/9Tdf
View: https://mail1.clearformat.com/vcard.php?uid=11&pid=10
Beta Test Advert: http://fwd.clearformat.com/9Tdd

------------9lCOoKqZccsfJhwva5P5Zx
Content-Type: multipart/related; boundary=----------9lCOoKqZccsfJh0nt7iooL

------------9lCOoKqZccsfJh0nt7iooL
Content-Type: text/html; charset="utf-8"
Content-ID: <op.1279273608106.306afd546d5da7de 10.0.0.88>
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
<HEAD></HEAD>
<body style="font-family:'DejaVu Sans Mono'; font-size:12px">
<div id="Headers"><div style="padding: 10px 0pt; background-color: rgb(37, 37,
37); color: rgb(255, 255, 255); font-size: 36px; font-family:
Verdana,Arial,Helvetica,sans-serif,'Trebuchet MS','Times New Roman';"><img
src="cid:logo.jpg 1368709366.25581" title="Logo"></div></div><div
id="Body_content"><div style="padding: 5px; font-family:
Arial,Helvetica,sans-serif; font-size: 12px;"><div><br>import
std.stdio;<br><br>struct State {<br>	string s; string getString() { return s;
}<br>	static State opCall(string s) {<br>		State ret;<br>		ret.s =
s;<br>		return ret;<br>	}<br>}<br><br>void main() {<br>	auto s =
State("adf");<br>	pragma(msg,
s.getString());<br>}<br></div><div><br></div><div>dmd Output: (line 14 is the
pragma statement)</div><div><br>struct.d(14): Error: variable s cannot be read
at compile time<br>struct.d(14): Error: cannot evaluate s.getString() at
compile time<br>s.getString()<br><br><br><br><br></div>
</div></div><div id="Signatures"><div style="padding: 3px; background-color:
rgb(238, 235, 230); color: rgb(86, 80, 68); line-height: 15px; font-size: 10px;
font-family: Verdana,Arial,Helvetica,sans-serif;">
<strong>Rory McGuire</strong><br>
R&D<br><br>
Tel : +27 (033) 386 7263<br>
<br />
Cell : +27 (082) 856 3646<br />
<strong>Email</strong>: <a href="mailto:rmcguire neonova.co.za"
style="font-size: 10px; font-family: Verdana,Arial,Helvetica,sans-serif; color:
rgb(86, 80, 68);">rmcguire neonova.co.za</a><br>
<strong>Website</strong>: <a href="http://cf.neonova.co.za/9Tdf"
style="font-size: 10px; font-family: Verdana,Arial,Helvetica,sans-serif; color:
rgb(86, 80, 68);">www.neonova.co.za</a><br>
<strong>VCard</strong>: <a
href="https://mail1.clearformat.com/vcard.php?uid=11&pid=10" style="font-size:
10px; font-family: Verdana,Arial,Helvetica,sans-serif; color: rgb(86, 80,
68);">View</a><br>
</div></div><div class="inline_ad"><a href="http://fwd.clearformat.com/9Tdd"
target="_blank"><img src="cid:ff2d54b.jpg 1039127786.16341" alt="Beta Test
Advert" border="0" class="inline_img"/></a></div>
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;
color:#999999; padding-top:10px;">
  <div align="justify">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.</div>
</div>
</body></HTML>
------------9lCOoKqZccsfJh0nt7iooL--

------------9lCOoKqZccsfJhwva5P5Zx--
Jul 16 2010
next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Jul 16 2010
prev sibling next sibling parent "Rory McGuire" <rmcguire neonova.co.za> writes:
On Fri, 16 Jul 2010 11:58:57 +0200, Lars T. Kyllingstad  
<public kyllingen.nospamnet> 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
Jul 16 2010
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Fri, 16 Jul 2010 12:12:38 +0200, Rory McGuire wrote:

 On Fri, 16 Jul 2010 11:58:57 +0200, Lars T. Kyllingstad
 <public kyllingen.nospamnet> 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
Jul 16 2010