digitalmars.D.learn - Assert Expressions???
- BCS (7/7) Jul 26 2007 this is just odd
- Gilles G. (4/15) Jul 26 2007 I tried this code and... it won't produce any error. I still don't unde...
- BCS (11/26) Jul 26 2007 in this case is shouldn't produce an error, however it shows that assert...
- Bill Baxter (8/28) Jul 26 2007 It looks like a function call and smells like a function call, so why
- Manfred Nowak (4/6) Jul 26 2007 Problem: what happens to an expression which has an `assert' as
- Sean Kelly (9/16) Jul 26 2007 I would guess that the assert() calls would be equivalent to an empty
- Bill Baxter (4/13) Jul 26 2007 Same thing as if you replace "assert(blah blah blah)" with "void", which...
- Robert Fraser (3/22) Jul 26 2007 Of course it won't produce any error. It's a conditional expression, so ...
this is just odd void main() { bool a; a ? assert(a) : assert(!a); } http://www.digitalmars.com/d/expression.html
Jul 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
Jul 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: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)) {...}this is just odd void main() { bool a; a ? assert(a) : assert(!a); } http://www.digitalmars.com/d/expression.html
Jul 26 2007
BCS wrote:Reply to Gilles G.,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. --bbI 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: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 expectthis is just odd void main() { bool a; a ? assert(a) : assert(!a); } http://www.digitalmars.com/d/expression.html
Jul 26 2007
Bill Baxter wroteso if it makes sense as an expression then might as well allow it to be used that wayProblem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler? -manfred
Jul 26 2007
Manfred Nowak wrote:Bill Baxter wroteI 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. Seanso if it makes sense as an expression then might as well allow it to be used that wayProblem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler?
Jul 26 2007
Manfred Nowak wrote:Bill Baxter wroteSame thing as if you replace "assert(blah blah blah)" with "void", which is what it evaluates to as an expression. --bbso if it makes sense as an expression then might as well allow it to be used that wayProblem: what happens to an expression which has an `assert' as subexpression, if -release is given to the compiler? -manfred
Jul 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
Jul 26 2007