Problem with using && as shorthand for if

Jonathan M Davis jmdavisprog at gmail.com
Fri Aug 20 14:17:53 PDT 2010


On Friday, August 20, 2010 14:00:22 div0 wrote:
> Then Andrei has taken leave of his senses and this is one situation
> where DMD is corrent and TDPL is wrong.
> 
> Half arsed, moronic shortcuts like that belong in scripting languages
> and shell environements, not serious programming languages.

Well, Andrei is definitely a fan of using D for small scripts, so arguments that 
something shouldn't be done because it's intended for scripting aren't going to 
fly with him. Personally, I find it a bit weird, but I don't really care. I 
probably won't code that way, but I don't mind that it's an option.

As for working in dmd, it _does_ work in dmd. Take this program for instance:

import std.stdio;
import std.array;

void main(string[] args)
{
    args.length > 1 && writeln("The program was called with arguments.");
}


It compiles just fine. If you don't pass any arguments to the program, then it 
prints nothing. If you do, then it prints

The program was called with arguments.


So, it looks to me like it generally works as intended. The fact that the case 
that started this thread doesn't work is a bug (quite possibly related to the 
fact that the condition can be determined at compile time).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list