Version declaration proposal

Michel Fortin michel.fortin at michelf.com
Wed Feb 11 04:24:31 PST 2009


On 2009-02-11 05:11:26 -0500, Don <nospam at nospam.com> said:

> version(2) {
>    pragma(msg, "surprise!");
> } else version(3) {
>    pragma(msg, "I bet you were expecting this.");
> }

This makes me think of something I tried to do when I started the Cocoa 
wrappers D/Objective-C bridge. Apple uses a couple of macros to make 
their headers work accross different versions of Mac OS X. If, for 
instance, a function has been added in Mac OS X version 10.4, it'll 
look like this:

	#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
		... new stuff ...
	#endif

While compiling on a Mac, you can define MAC_OS_X_VERSION_MAX_ALLOWED 
to control the availability of functions in the headers you include and 
thus be sure you're keeping yourself to what's available for the OS 
you're targeting, even if you're targetting an older OS.

I could have gone the way Don demonstrate, using a version number which 
fortunately includes everything below it, but then I'd be taking that 
number, preventing others from using it in their own applications. So I 
though of defining versions flags for each successive release of Mac OS 
X and ask everyone to define the version flags for all the releases up 
to what they're targeting... that's not very elegant either.

My conclusion is that the only way to do such fine-grained versionning 
in D is to use static ifs. If there were more than one version number 
allowed (so I don't eat up the only one available which someone else 
might want), this would mostly solve the problem.

The other variable you can set is MAC_OS_X_VERSION_MIN_REQUIRED and 
this one isn't likely to be possible in D in the current version of the 
language. It causes every function not available in the minimum 
required version to be weak-linked (so you can test for its existance 
and then use it). It also allow deprecated warnings when you are using 
functions that were deprecated, done this way:

	DEPRECATED_IN_MAC_OS_X_VERSION_10_2_AND_LATER void myFunction();

And any use of that function when MAC_OS_X_VERSION_MIN_REQUIRED is 
higher than 10.2 will give you a deprecation warning. The above is 
difficult to do in D:

	static if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2)
		deprecated void myFunction() { ... }
	else
		void myFunction() { ... }

Which forces you to duplciate the code.

I'd have liked to port that versionning system to by D wrappers, but it 
seems more trouble than it's worth, so I've just dropped the idea. 
(Another related problem is that adding/removing functions like this in 
a class changes the virtual table and makes the library incompatible 
with any program compiled with different version settings.)

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list