Jump to page: 1 213  
Page
Thread overview
Why version() ?
Feb 10, 2009
bobef
Feb 10, 2009
Trass3r
Feb 10, 2009
grauzone
Feb 10, 2009
Trass3r
Feb 10, 2009
Don
Feb 10, 2009
Derek Parnell
Feb 10, 2009
Don
Feb 10, 2009
Denis Koroskin
Feb 10, 2009
Daniel Keep
Feb 10, 2009
Ary Borenszweig
Feb 10, 2009
bearophile
Feb 10, 2009
Frits van Bommel
Feb 10, 2009
grauzone
Feb 16, 2009
Sergey Gromov
Feb 10, 2009
Nick Sabalausky
Feb 10, 2009
Lutger
Feb 10, 2009
Tim M
Feb 10, 2009
Tim M
Feb 10, 2009
Don
Feb 10, 2009
bearophile
Feb 10, 2009
Don
Feb 11, 2009
John Reimer
Feb 11, 2009
Walter Bright
Feb 10, 2009
Walter Bright
Feb 10, 2009
Derek Parnell
Feb 10, 2009
Walter Bright
Feb 11, 2009
Derek Parnell
Feb 11, 2009
Walter Bright
Feb 11, 2009
Sean Kelly
Feb 11, 2009
Sean Kelly
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Derek Parnell
Feb 11, 2009
Walter Bright
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Walter Bright
Feb 16, 2009
Sergey Gromov
Feb 11, 2009
BCS
Feb 11, 2009
Walter Bright
Feb 11, 2009
Walter Bright
Feb 12, 2009
Walter Bright
Feb 13, 2009
Jacob Carlborg
Feb 13, 2009
Nick Sabalausky
Feb 14, 2009
Jacob Carlborg
Feb 14, 2009
Nick Sabalausky
Feb 14, 2009
Yigal Chripun
Feb 14, 2009
Nick Sabalausky
Feb 15, 2009
Christian Kamm
Feb 11, 2009
Leandro Lucarella
Feb 11, 2009
Walter Bright
Feb 11, 2009
BCS
Feb 11, 2009
Walter Bright
Feb 12, 2009
BCS
Feb 12, 2009
Daniel Keep
Feb 10, 2009
Walter Bright
Feb 10, 2009
Denis Koroskin
Feb 11, 2009
Walter Bright
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Walter Bright
Feb 11, 2009
Walter Bright
Feb 11, 2009
Walter Bright
Feb 11, 2009
Walter Bright
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Walter Bright
Feb 11, 2009
Joel C. Salomon
Feb 11, 2009
Joel C. Salomon
Feb 11, 2009
Walter Bright
Feb 11, 2009
Joel C. Salomon
Feb 11, 2009
Walter Bright
Feb 11, 2009
Walter Bright
Feb 11, 2009
Bill Baxter
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Walter Bright
Feb 11, 2009
Sean Kelly
Feb 10, 2009
Nick Sabalausky
Feb 10, 2009
Walter Bright
Feb 10, 2009
Denis Koroskin
Feb 11, 2009
Walter Bright
Feb 11, 2009
Don
Feb 11, 2009
Walter Bright
Feb 11, 2009
Don
Feb 10, 2009
Tim M
Feb 11, 2009
Nick Sabalausky
Feb 10, 2009
grauzone
Feb 11, 2009
Daniel Keep
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Jason House
Feb 11, 2009
Walter Bright
Feb 11, 2009
Daniel Keep
Feb 11, 2009
Brian
Feb 11, 2009
Brian
Feb 11, 2009
Don
Feb 11, 2009
Nick Sabalausky
Feb 11, 2009
Jason House
Feb 11, 2009
Walter Bright
Feb 12, 2009
Sean Kelly
Feb 11, 2009
Ary Borenszweig
Feb 11, 2009
Ary Borenszweig
February 10, 2009
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
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
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
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
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
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
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
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
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
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;
}

« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11