Bug 3339 – template "is not evaluatable at compile time" error inconsistencies

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2009-09-22T01:33:00Z
Last change time
2015-06-09T01:26:47Z
Assigned to
nobody
Creator
chadjoan

Comments

Comment #0 by chadjoan — 2009-09-22T01:33:36Z
Version 1 of the program: --------------------------------------- pure string wham(string str) { return str; } template ouch(string val) { mixin(`const bool ouch = `~wham(val)~`;`); } static assert( ouch!("true") ); static assert( !ouch!("false") ); void main(){} --------------------------------------- This will compile just fine. I'm actually not sure if it should or not. By moving the wham function into the ouch template, the code will no longer compile; complaining about not being able to evaluate ouch at compile time. The changes make it look like so: --------------------------------------- template ouch(string val) { pure string wham(string str) { return str; } mixin(`const bool ouch = `~wham(val)~`;`); } static assert( ouch!("true") ); static assert( !ouch!("false") ); void main(){} --------------------------------------- main2.d(11): Error: static assert (ouch!("true")) is not evaluatable at compile time Another way to get the error is to store the result of wham into a constant and try to use it: --------------------------------------- pure string wham(string str) { return str; } template ouch(string val) { const string str = wham(val); mixin(`const bool ouch = `~str~`;`); // Same goes for `enum str = wham(val);` } static assert( ouch!("true") ); static assert( !ouch!("false") ); void main(){} --------------------------------------- main3.d(13): Error: static assert (ouch!("true")) is not evaluatable at compile time Again, I'm not actually sure whether these examples should compile or not. However, they should all be equivalent, yet one case will compile and the other ones will not.
Comment #1 by clugdbug — 2010-11-09T14:29:44Z
The syntax in cases 2 and 3 is wrong. It should be ouch!(true).ouch because there is more than one member in the template. When this is done, everything works correctly.