D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3339 - template "is not evaluatable at compile time" error inconsistencies
Summary: template "is not evaluatable at compile time" error inconsistencies
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-22 01:33 UTC by Chad Joan
Modified: 2015-06-09 01:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Chad Joan 2009-09-22 01:33:36 UTC
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 Don 2010-11-09 14:29:44 UTC
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.