[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] Or



Also, be aware of short-circuiting in Boolean operations:

a = 0;

if((a == 0) or: { "2nd test".postln; 0.5.coin}) { "true".postln } {
"false".postln };

(a == 0) is true, hence the result of "or" is guaranteed to be true.
The second branch does not execute.

if((a == 1) or: { "2nd test".postln; 0.5.coin}) { "true".postln } {
"false".postln };

(a == 0) is false, so we have to run the second test to get the result.

The second test has to be written as a function for short-circuiting to work.

Also, I use and: and or: rather than && and ||, so that the second
test functions will be inlined for speed.

Very useful for situations like this:

// a might be an array, or it might be nil
// if a is nil, we don't want to run the a[0] test
if(a.notNil and: { a[0] == 0 }) { /* do something */ };

hjh

--
James Harkins /// dewdrop world
jamshark70@xxxxxxxxxxxxxxxxx
http://www.dewdrop-world.net

"Come said the Muse,
Sing me a song no poet has yet chanted,
Sing me the universal."  -- Whitman