version(assert) defined variables

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Tue Dec 30 11:14:28 PST 2014


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

> 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?

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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141230/dd4ca8d5/attachment.sig>


More information about the Digitalmars-d mailing list