Thread overview
Assert Expressions???
Jul 26, 2007
BCS
Jul 26, 2007
Gilles G.
Jul 26, 2007
BCS
Jul 26, 2007
Bill Baxter
Jul 27, 2007
Manfred Nowak
Jul 27, 2007
Sean Kelly
Jul 27, 2007
Bill Baxter
Jul 26, 2007
Robert Fraser
July 26, 2007
this is just odd

void main()
{
 bool a;
 a ? assert(a) : assert(!a);
}

http://www.digitalmars.com/d/expression.html


July 26, 2007
I tried this code and... it won't produce any error.  I still don't understand why. Could you please report the (odd) explaination?

--
Gilles BCS Wrote:

> this is just odd
> 
> void main()
> {
>   bool a;
>   a ? assert(a) : assert(!a);
> }

> 
> http://www.digitalmars.com/d/expression.html
> 
> 

July 26, 2007
Reply to Gilles G.,

> I tried this code and... it won't produce any error.  I still don't
> understand why. Could you please report the (odd) explaination?
> 
> --
> Gilles
> BCS Wrote:
>> this is just odd
>> 
>> void main()
>> {
>> bool a;
>> a ? assert(a) : assert(!a);
>> }
>> http://www.digitalmars.com/d/expression.html
>> 

in this case is shouldn't produce an error, however it shows that assert is in fact an *expression* and not a statement as I would expect

this for instance, also works:

void main()
{
	int i = 5 + (assert(false), 6);
}

while witting this it just occurred to me why assert should be an expression. you can stuff it in logical expressions

while(ptr2ptr != null && (assert(*ptr2ptr != null), **ptr2ptr == 5)) {...}


July 26, 2007
Of course it won't produce any error. It's a conditional expression, so if X and Y are expressions, a ? X : Y will evaluate to X if a is true, and Y if a is false.

In this expression, this means that if a is true, the expression evaluates to assert(a), which is assert(true), which is always true. If a is false (as in the case with this code, because boolean values default to false when first initialized), the conditional will evaluate to assert(!a), which is assert(!false), which is also obviously true. a ? assert(a) : assert(!a); will _always_ evaluate to true.

Gilles G. Wrote:

> I tried this code and... it won't produce any error.  I still don't understand why. Could you please report the (odd) explaination?
> 
> --
> Gilles BCS Wrote:
> 
> > this is just odd
> > 
> > void main()
> > {
> >   bool a;
> >   a ? assert(a) : assert(!a);
> > }
> 
> > 
> > http://www.digitalmars.com/d/expression.html
> > 
> > 
> 

July 26, 2007
BCS wrote:
> Reply to Gilles G.,
> 
>> I tried this code and... it won't produce any error.  I still don't
>> understand why. Could you please report the (odd) explaination?
>>
>> -- 
>> Gilles
>> BCS Wrote:
>>> this is just odd
>>>
>>> void main()
>>> {
>>> bool a;
>>> a ? assert(a) : assert(!a);
>>> }
>>> http://www.digitalmars.com/d/expression.html
>>>
> 
> in this case is shouldn't produce an error, however it shows that assert is in fact an *expression* and not a statement as I would expect

It looks like a function call and smells like a function call, so why shouldn't it act like a function call?  Calling a function is an expression, so why shouldn't assert() be an expression too?  Seems perfectly reasonable to me.  Expressions can also be used in more places than statements, as you point out, so if it makes sense as an expression then might as well allow it to be used that way.

--bb
July 27, 2007
Bill Baxter wrote

> so if it makes sense as an expression then might as well allow it to be used that way

Problem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler?

-manfred
July 27, 2007
Manfred Nowak wrote:
> Bill Baxter wrote
> 
>> so if it makes sense as an expression then might as well allow it to be used that way
> 
> Problem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler?

I would guess that the assert() calls would be equivalent to an empty statement.  Kind of like how:

    if( sometimes() )
        assert( false );
    printf( "hello\n" );

should always print "hello," regardless of whether -release is specified or not.


Sean
July 27, 2007
Manfred Nowak wrote:
> Bill Baxter wrote
> 
>> so if it makes sense as an expression then might as well allow it to be used that way
> 
> Problem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler?
> 
> -manfred

Same thing as if you replace "assert(blah blah blah)" with "void", which is what it evaluates to as an expression.

--bb