February 10, 2009 Why version() ? | ||||
---|---|---|---|---|
| ||||
I was thinking... what is the point of version() ? It is so inflexible. There is no even version(!...). Why not "static if(version(DMD))" or static if(is(version == DMD))? Regards, bobef |
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | bobef schrieb:
> Why not "static if(version(DMD))" or static if(is(version == DMD))?
>
Cause nobody wants to write something ugly like that every time.
version(DMD) is nice and clear.
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | bobef wrote:
> I was thinking... what is the point of version() ? It is so inflexible. There is no even version(!...). Why not "static if(version(DMD))" or static if(is(version == DMD))?
>
> Regards,
> bobef
One of my pet peeves.
The only advantages of version() over static if() are:
1. You can pass version flags as command line parameters
2. It is shorter.
Some severe disadvantages of version():
1. They can't contain expressions (like "version(linux && mac)").
2. Typos are not catched! "version(linxu)" will just silently compile.
Proposal to solve these problems:
Remove "version()" and replace it by "static if()" (actually, it would make sense to turn version into an alias for static if). Introduce const declarations, which get their value from commandline:
//command line for dmd to set the constant cLinux
dmd -version cLinux=true
//define a const variable, whose value is taken from the commandline
//they always need to be declared, the compiler shouldn't just
//arbitrarily introduce new symbols into the modules' scope!
//(exact syntax needs to be discussed)
const bool cLinux = $cmdarg;
//instead of version(linux)
//unlike version, arbitrary expressions are allowed
static if(cLinux) {
//Linux code goes here
}
static if(cLinxu) { //oops, typo, the compiler will loudly complain!
As an additional feature, you could allow passing of additional data types instead of only bool. Strings, for example.
Unlike version(), cLinux always needs to be defined on the command line. I don't consider this a disadvantage.
I'd also leave debug() as it is.
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | bobef wrote: > I was thinking... what is the point of version() ? It is so inflexible. There is no even version(!...). Why not "static if(version(DMD))" or static if(is(version == DMD))? > > Regards, > bobef What would be the difference except more verbosity? You can use version(x) { } else { } The rationale of version() is a simple, coarse grained conditional compilation of features. It is not meant for metaprogramming like static if is. |
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone schrieb:
> Some severe disadvantages of version():
> 1. They can't contain expressions (like "version(linux && mac)").
version(linux)
version(mac)
{
}
But yeah, that's a feature that could be added.
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone:
> Unlike version(), cLinux always needs to be defined on the command line. I don't consider this a disadvantage.
I think I don't like that.
But I may like something like this:
static if (__version__ == DMD) {...}
more than:
version(DMD) {...}
It's longer, but it doesn't need the version keyword, it's more uniform and easy anyway to remember and use, and it can be used in more refined ways.
I may even appreciate other boolean constants like:
static if (__debug__) {...}
static if (__unittest__) {...}
static if (__ismain__) {...}
Where bud and similar tools define __ismain__ as true only inside the main module.
Bye,
bearophile
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | Trass3r wrote:
> grauzone schrieb:
>> Some severe disadvantages of version():
>> 1. They can't contain expressions (like "version(linux && mac)").
>
> version(linux)
> version(mac)
> {
> }
>
> But yeah, that's a feature that could be added.
A&&B is not so terrible, since you can do it by nesting, as above.
The big problem is A || B. It creates a mess.
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | On Wed, 11 Feb 2009 00:28:21 +1300, bobef <bobef@nospam-abv.bg> wrote:
> I was thinking... what is the point of version() ? It is so inflexible. There is no even version(!...). Why not "static if(version(DMD))" or static if(is(version == DMD))?
>
> Regards,
> bobef
You could try something like this:
module Vers;
template vers(char[] V)
{
mixin("version(" ~ V ~ ")
{
const bool vers = true;
}
else
{
const bool vers = false;
}");
}
int main()
{
static if(vers!("X86_64"))
{
long a;
pragma(msg, "64 bit");
}
else
{
int a;
pragma(msg, "32 bit");
}
a = 2;
return a;
}
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone wrote:
> Some severe disadvantages of version():
> 1. They can't contain expressions (like "version(linux && mac)").
Unless Apple has plans to s/BSD/Linux/ in MacOS that I'm not aware of, version(linux || mac) would probably be more useful...
Allowing boolean expressions (using only version identifiers) in version() would be a very welcome feature.
I'm not sure about the rest of your post though.
|
February 10, 2009 Re: Why version() ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim M | It gets even better with constants and works in static asserts too: module Vers; template vers(char[] V) { mixin("version(" ~ V ~ ") { const bool vers = true; } else { const bool vers = false; }"); } const DigitalMars = "DigitalMars"; const X86 = "X86"; const X86_64 = "X86_64"; const Windows = "Windows"; const Win32 = "Win32"; const Win64 = "Win64"; const LittleEndian = "LittleEndian"; const BigEndian = "BigEndian"; const D_Coverage = "D_Coverage"; const D_Ddoc = "D_Ddoc"; const D_InlineAsm_X86 = "D_InlineAsm_X86"; const D_InlineAsm_X86_64 = "D_InlineAsm_X86_64"; const D_LP64 = "D_LP64"; const D_PIC = "D_PIC"; const none = "none"; const all = "all"; int main() { const bool forceLong = false; static if(vers!(X86_64) || forceLong) { long a; pragma(msg, "64 bit"); } else { int a; pragma(msg, "32 bit"); } static assert(vers!(LittleEndian) || vers!(BigEndian), "You are a weird endian"); a = 2; return a; } |
Copyright © 1999-2021 by the D Language Foundation