August 20, 2010 Re: Problem with using && as shorthand for if | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Friday, August 20, 2010 15:32:16 bearophile wrote:
> Jonathan M Davis:
> > 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.
>
> Even small scripts need to be tidy. And I don't like that stupid shortcut in a language that tries to be safe as D. Andrei is wrong here.
>
> A problem with similar shortcuts is that they save you few chars and they look handy, but sometimes they later come back to bite your ass. Perl is a good example of this.
>
> Bye,
> bearophile
It's essentially the same as putting an entire if statement on one line, which I'm not a fan of doing, but there are plenty of people who do.
- Jonathan M Davis
|
September 07, 2010 Re: Problem with using && as shorthand for if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Iain Buclaw | On Fri, 20 Aug 2010 20:33:43 +0000, Iain Buclaw wrote:
> == Quote from Ersin Er (ersin.er@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
And the relevance of that to the OP's post which noted that
1 == 2 && writeln("1 = 2");
produces an error message complaining about void where a scalar is expected is what?
Also irrelevant are statements that Andrei is wrong or even has taken leave of his senses merely for accurately describing in his book a particular bit of D's semantics that he may or may not have had any role in crafting.
|
Copyright © 1999-2021 by the D Language Foundation