www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to check for a pre-existing type?

reply Jay <Jay_member pathlink.com> writes:
Hello, 
 
I had converted the following C code fragment: 
 
#ifndef MYTYPE
#define MYTYPE 
typedef int mytype; 
#endif 
 
into the following D code
fragment: 
 
version (MYTYPE) { 
} else { 
	version = MYTYPE; 
	alias int
mytype; 
} 
 
which used to work until I moved from DMD version 0.121 to 0.127.
Now it 
reports an error of the form: 
 
mytype.d:3: version MYTYPE defined
after use 
 
The changelog of DMD version 0.127 states that forward references
of version 
and debug identifiers aren't allowed anymore. It doesn't cite a
reason and 
searching for one didn't yield anything. 
 
I thought I may use the
"iftype" construct but the compiler says it is 
deprecated in favour of "static
if", but "static if" isn't allowed at 
global/module scope. 
 
I have two
questions: 
 
1. Does anybody know of a reason for the restriction on forward
references of 
version and debug identifiers? If yes, can you please point me to
any relevant 
discussion. 
 
2. Can anybody please help me with my problem of
identifying a pre-exisiting 
type and defining it if it doesn't exist? I'd
appreciate a simple example. 
 
Hopefully, 
Jay 
Jul 04 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jay" <Jay_member pathlink.com> wrote in message 
news:dacjds$2037$1 digitaldaemon.com...
 I thought I may use the
 "iftype" construct but the compiler says it is
 deprecated in favour of "static
 if", but "static if" isn't allowed at
 global/module scope.

iftype() has been replaced with the "is" expression. You can check if a type has been defined by just placing the type as the parameter to "is": if(is(mytype)) { ... } Since is() is evaluated at compile time, it can be used in static if() statements. However, the problem, as you said, is that static if() is not allowed at global scope. Perhaps this limitation could be removed, as static if() is more like a conditional compilation expression than anything else, and could be allowed at global scope.
 1. Does anybody know of a reason for the restriction on forward
 references of
 version and debug identifiers?

I believe it was because version statements in imported modules were affecting compilation, as well as files which changed versions in the middle of the file didn't work correctly, as the version was being applied to all code. So now it works kind of like a public: label - it only affects code that follows.
Jul 04 2005