Thread overview
No parenthesis for assert?
Jan 12, 2012
Manfred Nowak
Jan 12, 2012
Peter Alexander
Jan 14, 2012
Stewart Gordon
Jan 16, 2012
Stewart Gordon
January 12, 2012
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
January 12, 2012
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
January 12, 2012
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
January 12, 2012
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?
January 12, 2012
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
January 14, 2012
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.
January 14, 2012
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
January 16, 2012
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.