www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - version(assert) defined variables

reply Shachar Shemesh <shachar weka.io> writes:
Hi all,

Please consider the following program:
int main()
{
     version(assert) int var;

     assert(var == 0);

     return 0;
}

When compiled with "dmd test.d", it passes without a problem. When 
compiled with "dmd test.d -release", it produces the following error:
test.d(5): Error: undefined identifier var

This makes no sense. In order to make the program compile, I need to do 
the (quite nonsensical):

version(assert) assert(var == 0);

Is this on purpose?

Shachar
Dec 30 2014
parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tue, 30 Dec 2014 14:07:41 +0200
Shachar Shemesh via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 Hi all,
=20
 Please consider the following program:
 int main()
 {
      version(assert) int var;
=20
      assert(var =3D=3D 0);
=20
      return 0;
 }
=20
 When compiled with "dmd test.d", it passes without a problem. When=20
 compiled with "dmd test.d -release", it produces the following error:
 test.d(5): Error: undefined identifier var
=20
 This makes no sense. In order to make the program compile, I need to do=20
 the (quite nonsensical):
=20
 version(assert) assert(var =3D=3D 0);
=20
 Is this on purpose?
of course. D does exactly what you tell it to do. `assert()` is not a kind of macro, so it MUST compile, whether compiler will generate the code for it later or will just drop it. i.e. it must be correct and pass all semantic analysis phases and symbol resolving phases. please, remember, that there is NO c-like macros in D. just don't expect something to simply ignore it's input. this is true for `version` blocks too: "ignored" `version` block must not confuse parser. only parser this time, but you still can't throw arbitrary nonsence with unbalanced parens there and expect the code to compiles ok.
Dec 30 2014