more enum and pragma troubles

JS js.mdnq at gmail.com
Fri Aug 16 23:03:46 PDT 2013


On Saturday, 17 August 2013 at 05:22:53 UTC, captaindet wrote:
> are these bugs or expected behavior? (2 issues, see code below)
>
> i thought enums are just compile time copy&paste magic so i 
> cannot think of a reason why it should not be available at 
> compile time...
>
> if buggy, what is broken, enum (really bad) or pragma(msg,...) 
> (worse enough)?
> and are they in bugzilla already? (looked but nothing caught my 
> eye)
>
> if expected behavior, can someone pls explain.
>
> cheers /det
>
>
> import std.stdio;
>
> enum Test{
> 	test2 = 2,
> 	test4 = 4
> }
>
> enum foo = Test.test2;
> Test bar = Test.test4;
>
> pragma( msg, foo );		// why does it print: cast(Test)2
> 				// it should be: test2
> 				// or at least simply: 2
>

It is 2! Notice the 2 at the end? The cast is just mumbo jumbo 
due to the the compiler deals with the stuff. You can remove the 
casts manually if you don't like them.

> //  pragma( msg, bar );		// Error: variable bar cannot be read 
> at compile time
>

Because bar is not a compile time constant. It is an runtime 
object. pragma can't display certain things that it doesn't know 
about due to the way the compiler works. It's unfortunate in some 
cases that it simply doesn't work correctly, in others, such as 
this, it works as expected.

You could argue that the compiler should know that bar is known 
at compile time up to the pragma, and I would agree... but the 
compiler simply isn't that smart.


> void main(){
> 	writeln( typeof(foo).stringof, " = ", foo );	
> 		// prints as expected: Test = test2
> 	writeln( typeof(bar).stringof, " = ", bar );	
> 		// prints as expected: Test = test4
> }

these print the value at runtime. In this case the compiler is 
already out of the picture.

Basically pragmas are evaluated during compile time, if the 
compiler can't figure out the value(or is too stupid) then it 
can't print them and throws an error.

In fact, even in CTFE stuff pragma doesn't function like one 
would hope. This again, is due to the way the compiler works. 
Just a quirky and unfortunate way that the d compiler works.





More information about the Digitalmars-d-learn mailing list