www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - No parenthesis for assert?

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

Is there any reason parentheses are required on asserts?

It would be much neater to be able to write:

assert x != 0;

than:

assert(x != 0);

By making parentheses optional, it would also feel more like a language 
feature than some sort of function. I realize optional parentheses 
wouldn't be useful if you're passing a message to assert, but I think 
for all other use cases, it would be a nice thing to have.

- Alex
Jan 12 2012
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Alex Rønne Petersen wrote:
 Is there any reason parentheses are required
Yes.
 for all other use cases, it would be a nice thing to have. 
Maybe, but only when no `assert'-expression(!) is evaluated ever ... Because making parenthesis optional for `assert' would introduce ambiguities. -manfred
Jan 12 2012
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 12-01-2012 16:57, Manfred Nowak wrote:
 Alex Rønne Petersen wrote:
 Is there any reason parentheses are required
Yes.
 for all other use cases, it would be a nice thing to have.
Maybe, but only when no `assert'-expression(!) is evaluated ever ... Because making parenthesis optional for `assert' would introduce ambiguities. -manfred
Right, it would only make sense when it's used as a statement expression. In all other cases, it wouldn't work, of course. - Alex
Jan 12 2012
parent reply Peter Alexander <peter.alexander.au gmail.com> writes:
On 12/01/12 4:34 PM, Alex Rønne Petersen wrote:
 On 12-01-2012 16:57, Manfred Nowak wrote:
 Alex Rønne Petersen wrote:
 Is there any reason parentheses are required
Yes.
 for all other use cases, it would be a nice thing to have.
Maybe, but only when no `assert'-expression(!) is evaluated ever ... Because making parenthesis optional for `assert' would introduce ambiguities. -manfred
Right, it would only make sense when it's used as a statement expression. In all other cases, it wouldn't work, of course. - Alex
assert can also have a message: assert(x == 0, "What's going on!?"); This would look odd without parentheses. assert x == 0, "What's going on!?"; Not to mention that it would be ambiguous with the comma operator: assert (a, b); // is this the comma operator, or two arguments?
Jan 12 2012
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 12-01-2012 22:35, Peter Alexander wrote:
 On 12/01/12 4:34 PM, Alex Rønne Petersen wrote:
 On 12-01-2012 16:57, Manfred Nowak wrote:
 Alex Rønne Petersen wrote:
 Is there any reason parentheses are required
Yes.
 for all other use cases, it would be a nice thing to have.
Maybe, but only when no `assert'-expression(!) is evaluated ever ... Because making parenthesis optional for `assert' would introduce ambiguities. -manfred
Right, it would only make sense when it's used as a statement expression. In all other cases, it wouldn't work, of course. - Alex
assert can also have a message: assert(x == 0, "What's going on!?"); This would look odd without parentheses. assert x == 0, "What's going on!?"; Not to mention that it would be ambiguous with the comma operator: assert (a, b); // is this the comma operator, or two arguments?
I did point out that it would only really make sense for asserts without messages. - Alex
Jan 12 2012
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 12/01/2012 22:56, Alex Rønne Petersen wrote:
 On 12-01-2012 22:35, Peter Alexander wrote:
<snip>
 Not to mention that it would be ambiguous with the comma operator:

 assert (a, b); // is this the comma operator, or two arguments?
I did point out that it would only really make sense for asserts without messages.
Therein lies the point. If we changed the AssertExpression grammar to AssertExpression: assert ( AssignExpression ) assert ( AssignExpression , AssignExpression ) assert AssignExpression then the form assert ( AssignExpression , AssignExpression ) would become ambiguous, because it could be parsed as assert ( AssignExpression , CommaExpression ) assert ( CommaExpression ) assert ( Expression ) assert AssignExpression In some cases it's resolved by an "if it's parseable as X, it's X" rule, but this would create fragility because of the possibility of a CommaExpression of three or more terms. As such, it would be a case of explicitly disallowing assert ( CommaExpression ) in the grammar. Stewart.
Jan 14 2012
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 14-01-2012 13:59, Stewart Gordon wrote:
 On 12/01/2012 22:56, Alex Rønne Petersen wrote:
 On 12-01-2012 22:35, Peter Alexander wrote:
<snip>
 Not to mention that it would be ambiguous with the comma operator:

 assert (a, b); // is this the comma operator, or two arguments?
I did point out that it would only really make sense for asserts without messages.
Therein lies the point. If we changed the AssertExpression grammar to AssertExpression: assert ( AssignExpression ) assert ( AssignExpression , AssignExpression ) assert AssignExpression then the form assert ( AssignExpression , AssignExpression ) would become ambiguous, because it could be parsed as assert ( AssignExpression , CommaExpression ) assert ( CommaExpression ) assert ( Expression ) assert AssignExpression In some cases it's resolved by an "if it's parseable as X, it's X" rule, but this would create fragility because of the possibility of a CommaExpression of three or more terms. As such, it would be a case of explicitly disallowing assert ( CommaExpression ) in the grammar. Stewart.
Point taken; I now see what you're getting at. I tend to forget the existence of the comma operator when considering these things... I wish we wouldn't have it in the language; it seems to be more trouble than it's worth (in more ways than one). -- - Alex
Jan 14 2012
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
On 14/01/2012 15:32, Alex Rønne Petersen wrote:
 As such, it would be a case of explicitly disallowing

 assert ( CommaExpression )

 in the grammar.
Correction: because of the way the grammar works, it would be a case of explicitly disallowing assert ( AssignExpression , CommaExpression ) Stewart.
Jan 15 2012