What's the D way of application version numbers?
Meta
jared771 at gmail.com
Mon Jan 13 12:46:30 PST 2014
On Monday, 13 January 2014 at 19:18:17 UTC, Russel Winder wrote:
> On Mon, 2014-01-13 at 15:05 +0000, Dejan Lekic wrote:
> […]
>> I simply define version in my main module (module which
>> contains the main() function). I am planning to submit a DIP
>> about something that is related to this. - I think we really
>> need a way to specify version of package, and maybe even
>> version of a module (similar to how the new Java module system
>> works - see project Jigsaw). D does not offer this. I humbly
>> believe it should be part of the language as it could be used
>> also for building versions of dynamic libraries.
>
> For the version number to be available to SCons it cannot be
> embedded in
> the D source code. (Unless I am missing some really useful
> trick. :-)
>
>> Anyway, back to the topic.
>> Say my main() function is in the org.dlang.myapp module.
>> If I want to have information about version of the artifact,
>> my myapp.d starts with the following two lines of D code:
>> module org.dlang.myapp;
>> version = 1_3_21_4; // major_minor_micro_qualifier
>
> Python uses a tuple:
>
> (<major>, <minor>, <bugfix>, <annotation>)
>
> which seems to work very well: it needs very little
> reprocessing to be
> used in most contexts.
>
>> Naturally, build tool looks for the version line when I need
>> it.
template DeclareVersion(T...)
{
static if (T.length == 4)
{
alias Version = T;
}
else
{
static assert(false, "Version declaration must be of the
form Version!(major, minor, bugfix, annotation)");
}
}
enum Version
{
major = 0,
minor = 1,
bugfix = 2,
annotation = 3,
}
//main.d
alias AppVersion = DeclareVersion!("1", "3", "21", "4");
import std.stdio;
void main()
{
writeln(AppVersion[Version.major]);
}
Kind of neat, it's even accessible at compile time and gets
compiled into the source.
More information about the Digitalmars-d-learn
mailing list