www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assert Expressions???

reply BCS <ao pathlink.com> writes:
this is just odd

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

http://www.digitalmars.com/d/expression.html
Jul 26 2007
parent reply Gilles G. <schaouette free.fr> writes:
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
 
 

Jul 26 2007
next sibling parent reply BCS <ao pathlink.com> writes:
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)) {...}
Jul 26 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Jul 26 2007
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
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
Jul 26 2007
next sibling parent Sean Kelly <sean f4.ca> writes:
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
Jul 26 2007
prev sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Jul 26 2007
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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
 
 


Jul 26 2007