digitalmars.D.learn - Assert Expressions???
- BCS <ao pathlink.com> Jul 26 2007
- Gilles G. <schaouette free.fr> Jul 26 2007
- BCS <ao pathlink.com> Jul 26 2007
- Bill Baxter <dnewsgroup billbaxter.com> Jul 26 2007
- Manfred Nowak <svv1999 hotmail.com> Jul 26 2007
- Sean Kelly <sean f4.ca> Jul 26 2007
- Bill Baxter <dnewsgroup billbaxter.com> Jul 26 2007
- Robert Fraser <fraserofthenight gmail.com> Jul 26 2007
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: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
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
Bill Baxter wroteso 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
Manfred Nowak wrote:Bill Baxter wroteso 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
Manfred Nowak wrote:Bill Baxter wroteso 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
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









Sean Kelly <sean f4.ca> 