Problem with using && as shorthand for if

Iain Buclaw ibuclaw at ubuntu.com
Fri Aug 20 13:33:43 PDT 2010


== Quote from Ersin Er (ersin.er at gmail.com)'s article
> Hi,
> The following code compiles and outputs "1 = 1" as expected:
> 1 == 1 && writeln("1 = 1");
> However, the following code fails to compile (although it should not):
> 1 == 2 && writeln("1 = 2");
> The error is as follows:
> Error: integral constant must be scalar type, not void
> What I expect that the second code should also compile and output nothing when
executed.
> Am I missing something?
> Thanks.

Because you are dealing with literals here, it's best to assume the compiler
will try to evaluate and compile down the code you write.

The first example you give will be optimized down to just

    writeln("1 = 1");

Whilst your second example is simply

   false;


Regards


More information about the Digitalmars-d-learn mailing list