www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with using && as shorthand for if

reply Ersin Er <ersin.er gmail.com> writes:
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.
Aug 20 2010
next sibling parent Ersin Er <ersin.er gmail.com> writes:
I forgot to mention that I am using dmd 2.048 on Ubuntu 10.04
64-bit.
Aug 20 2010
prev sibling next sibling parent reply div0 <div0 sourceforge.net> writes:
On 20/08/2010 20:59, Ersin Er wrote:
 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.

The return type of writeln is void. You can't && with void. You are asking 'is X true AND <something which can't return true or false> is true' which is clearly nonesense. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Aug 20 2010
next sibling parent reply Jonathan M Davis <jmdavisprog gmail.com> writes:
On Friday, August 20, 2010 13:06:11 div0 wrote:
 On 20/08/2010 20:59, Ersin Er wrote:
 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.

The return type of writeln is void. You can't && with void. You are asking 'is X true AND <something which can't return true or false> is true' which is clearly nonesense.

It's legal according to TDPL. It seems to be intended to be used as a shorthand for if. So, stuff like condition && writeln("my output"); are supposed to be perfectly legal as bizarre as that may seem. I don't believe that it would be legal to do if(condition && writeln("my output")) { } since the result fed to if must be a bool, but a statement doesn't need to result in bool, so apparently you can use && with a void function in a statement. It's just that the void function must be last. - Jonathan M Davis
Aug 20 2010
parent reply div0 <div0 sourceforge.net> writes:
On 20/08/2010 21:16, Jonathan M Davis wrote:

 It's legal according to TDPL. It seems to be intended to be used as a shorthand
 for if. So, stuff like

 condition&&  writeln("my output");

 are supposed to be perfectly legal as bizarre as that may seem. I don't believe
 that it would be legal to do

 if(condition&&  writeln("my output"))
 {
 }

 since the result fed to if must be a bool, but a statement doesn't need to
 result in bool, so apparently you can use&&  with a void function in a
 statement. It's just that the void function must be last.

 - Jonathan M Davis

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. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Aug 20 2010
parent bearophile <bearophileHUGS lycos.com> writes:
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
Aug 20 2010
prev sibling next sibling parent Ersin Er <ersin.er gmail.com> writes:
--0016368336f87be3b0048e46f4d3
Content-Type: text/plain; charset=UTF-8

On Fri, Aug 20, 2010 at 23:06, div0 <div0 sourceforge.net> wrote:

 On 20/08/2010 20:59, Ersin Er wrote:

 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.

The return type of writeln is void. You can't && with void. You are asking 'is X true AND <something which can't return true or false> is true' which is clearly nonesense.

Then the first code should not compile too. By the way here is what Andrei wrote in his latest book for the expression a&&b: """ * If the type of b is not void, then the expression has type bool. If a is nonzero, the expression evaluates b and yields true if and only if b is nonzero. Otherwise, the expression evaluates to false. * If b has type void, the expression has type void as well. If a is nonzero, b is evaluated. Otherwise, b is not evaluated. Using && with a void expression on the right-hand side is useful as shorthand for an if statement: string line; ... line == "#\n" && writeln("Got a # successfully"); """ So I expect my code to compile as well.
 --
 My enormous talent is exceeded only by my outrageous laziness.
 http://www.ssTk.co.uk

-- Ersin ER http://metasolid.com --0016368336f87be3b0048e46f4d3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Fri, Aug 20, 2010 at 23:06, div0 <span dir=3D= "ltr">&lt;<a href=3D"mailto:div0 sourceforge.net">div0 sourceforge.net</a>&= gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 = 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> On 20/08/2010 20:59, Ersin Er wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi,<br> <br> The following code compiles and outputs &quot;1 =3D 1&quot; as expected:<br=

1 =3D=3D 1&amp;&amp; =C2=A0writeln(&quot;1 =3D 1&quot;);<br> <br> However, the following code fails to compile (although it should not):<br> <br> 1 =3D=3D 2&amp;&amp; =C2=A0writeln(&quot;1 =3D 2&quot;);<br> <br> The error is as follows:<br> <br> Error: integral constant must be scalar type, not void<br> <br> What I expect that the second code should also compile and output nothing w= hen executed.<br> <br> Am I missing something?<br> <br> Thanks.<br> </blockquote> <br> The return type of writeln is void.<br> You can&#39;t &amp;&amp; with void.<br> <br> You are asking<br> <br> &#39;is X true AND &lt;something which can&#39;t return true or false&gt; i= s true&#39;<br> <br> which is clearly nonesense.</blockquote><div><br></div><div>Then the first = code should not compile too. By the way here is what Andrei wrote in his la= test book for the expression=C2=A0a&amp;&amp;b:</div><div><br></div><div>&q= uot;&quot;&quot;</div> <div><div>* If the type of b is not void, then the expression has type bool= . If a is nonzero, the</div><div>expression evaluates b and yields true if = and only if b is nonzero. Otherwise, the</div><div>expression evaluates to = false.</div> <div><br></div><div>* If b has type void, the expression has type void as w= ell. If a is nonzero, b is evaluated.</div><div>Otherwise, b is not evaluat= ed.</div></div><div><br></div><div>Using &amp;&amp; with a void expression = on the right-hand side is useful as shorthand for an</div> <div>if statement:</div><div><br></div><div>string line;</div><div>...</div=
<div>line =3D=3D &quot;#\n&quot; &amp;&amp; writeln(&quot;Got a # successf=

ect my code to compile as well.</div> <div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex= ;border-left:1px #ccc solid;padding-left:1ex;"><font color=3D"#888888"><br> -- <br> My enormous talent is exceeded only by my outrageous laziness.<br> <a href=3D"http://www.ssTk.co.uk" target=3D"_blank">http://www.ssTk.co.uk</= a><br> </font></blockquote></div><br><br clear=3D"all"><br>-- <br>Ersin ER<div><a = href=3D"http://metasolid.com" target=3D"_blank">http://metasolid.com</a></d= iv><br> --0016368336f87be3b0048e46f4d3--
Aug 20 2010
prev sibling parent Ersin Er <ersin.er gmail.com> writes:
--0016e659f5e6b8a03d048e47b954
Content-Type: text/plain; charset=UTF-8

On Sat, Aug 21, 2010 at 00:00, div0 <div0 sourceforge.net> wrote:

 On 20/08/2010 21:16, Jonathan M Davis wrote:

  It's legal according to TDPL. It seems to be intended to be used as a
 shorthand
 for if. So, stuff like

 condition&&  writeln("my output");

 are supposed to be perfectly legal as bizarre as that may seem. I don't
 believe
 that it would be legal to do

 if(condition&&  writeln("my output"))
 {
 }

 since the result fed to if must be a bool, but a statement doesn't need to
 result in bool, so apparently you can use&&  with a void function in a
 statement. It's just that the void function must be last.

 - Jonathan M Davis

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.

If Andrei is wrond and DMD is right, then the first example should not have compiled too..
 --
 My enormous talent is exceeded only by my outrageous laziness.
 http://www.ssTk.co.uk

-- Ersin Er http://metasolid.com --0016e659f5e6b8a03d048e47b954 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sat, Aug 21, 2010 at 00:00, div0 <spa= n dir=3D"ltr">&lt;<a href=3D"mailto:div0 sourceforge.net">div0 sourceforge.= net</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"mar= gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"im">On 20/08/2010 21:16, Jonathan M Davis wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> It&#39;s legal according to TDPL. It seems to be intended to be used as a s= horthand<br> for if. So, stuff like<br> <br> condition&amp;&amp; =C2=A0writeln(&quot;my output&quot;);<br> <br> are supposed to be perfectly legal as bizarre as that may seem. I don&#39;t= believe<br> that it would be legal to do<br> <br> if(condition&amp;&amp; =C2=A0writeln(&quot;my output&quot;))<br> {<br> }<br> <br> since the result fed to if must be a bool, but a statement doesn&#39;t need= to<br> result in bool, so apparently you can use&amp;&amp; =C2=A0with a void funct= ion in a<br> statement. It&#39;s just that the void function must be last.<br> <br> - Jonathan M Davis<br> </blockquote> <br></div> Then Andrei has taken leave of his senses and this is one situation where D= MD is corrent and TDPL is wrong.<br> <br> Half arsed, moronic shortcuts like that belong in scripting languages and s= hell environements, not serious programming languages.</blockquote><div><br=
</div><div>If Andrei is wrond and DMD is right, then the first example sho=

<div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8= ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div class=3D"h5"> -- <br> My enormous talent is exceeded only by my outrageous laziness.<br> <a href=3D"http://www.ssTk.co.uk" target=3D"_blank">http://www.ssTk.co.uk</= a><br> </div></div></blockquote></div><br><br clear=3D"all"><br>-- <br>Ersin Er<di= v><a href=3D"http://metasolid.com" target=3D"_blank">http://metasolid.com</= a></div><br> --0016e659f5e6b8a03d048e47b954--
Aug 20 2010
prev sibling next sibling parent reply Iain Buclaw <ibuclaw ubuntu.com> writes:
== 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

 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
Aug 20 2010
parent Jay Byrd <JayByrd rebels.com> writes:
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

 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.
Sep 06 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
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
Aug 20 2010
prev sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
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
Aug 20 2010