forcing evaluation in if()
Antonio
antonio at abrevia.net
Fri Nov 17 09:24:18 PST 2006
> Yup. I just assumed the operands were bools in the first place..
>
> Other workarounds include
>
> cast(bool)a & cast(bool)b
> !!a & !!b
> a?b?1:0:b?0:0 // just kidding
>
> Though, I think that if I wanted to always execute both sides, I'd put
> that execution outside the if expression, it makes it far more obvious
> what's going on.
>
> auto a = something();
> auto b = somethingElse();
> if (a && b) {
> ...
> }
>
>
> xs0
It's true: the "correct" way is not to put "process" into "decission
blocks".
But there is some cases where I prefer to do something like
Recordset rd = new Recordset()
Recordset rd2 = new Recordset()
while( rd.Read() &&& rd2.Read() ) {
doSomething( rd, rd2 );
}
// at this point, the number of Read()'s MUST BE the same
// in rd and rd2
it's equivalent to:
RecordSet rd = new RecordSet()
RecordSet rd2 = new RecordSet()
bool exists = rd.Read();
bool exists2 = rd2.Read();
while( exists && exists2 ) {
doSomething( rd, rd2 );
exists = rd.Read();
exists2 = rd2.Read();
}
Ok... you say it could be wrote as
while( rd.Read() & rd2.Read() ) {
But & is "bit" tester, not a Logic evaluator... We are talking about a
hight level programming language... we can use "invariants"
"preconditions" "postconditions" and.... SURPRISSE... I have to use a
"bit test" for "logic comparations"... no thanks..
Otherwhise... bit test could be "optimized" (if left side is 0, "&"
allways evaluates to 0).
Sorry... I continue with my very poor english :-(.
More information about the Digitalmars-d
mailing list