Page 1 of 1
Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 5:49 pm
by Eoghan
Hi everyone - sorry if this is a very basic question, but I wondered if there's an official position on this.
Can we count on LiveCode's 'if' control structure always doing lazy evaluation?
... or would the following code be bad practice?
Code: Select all
if (tMyArray is not empty) and (tMyArray[tMyKey] is not empty) then
# do something with tMyArray[tMyKey]
end if
In the above stack, the evaluation of the 'if' is lazy (if the 'firstTest value' is 'false', then the secondTest() function isn't called) - but I'd better not rely on that functionality if LiveCode might change to eager evaluation at some point.
Thanks
Eoghan
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 5:55 pm
by Klaus
HI Eoghan,
not sure I understand the problem!?
If the first expression = FALSE why check the other expression at all, lazy evaluation or not?
Does not make any logical sense to me
Best
Klaus
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 5:57 pm
by Klaus
P.S.
In your case you only need to check one condition:
Code: Select all
if tMyArray[tMyKey] <> empty then
# do something with tMyArray[tMyKey]
end if

Re: Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 6:11 pm
by Eoghan
Hi Klaus,
When I was studying Comp Sci (nearly 20 years ago ...), they always said that the following code was the "Wally trap":
Code: Select all
if (tMyArray is not empty) and (tMyArray[tMyKey] is not empty) then
They told us to test each part of that 'if' statement separately, in case we came across a programming language that would execute both parts, even if the first part was false.
As you say, logically the above code should be fine - but I wondered if it was officially stated anywhere that it is alright to use in LiveCode.
Ah, thanks for your second comment! So 'tMyArray[tMyKey] <> empty' won't cause problems if tMyArray is empty (it won't try and do a C-type dereference of a null pointer)?
Cheers,
Eoghan.
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 6:58 pm
by Klaus
Hi Eoghan,
Eoghan wrote:Ah, thanks for your second comment! So 'tMyArray[tMyKey] <> empty' won't cause problems if tMyArray is empty (it won't try and do a C-type dereference of a null pointer)?
no it won't
Best
Klaus
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sat Apr 05, 2014 7:43 pm
by Eoghan
Excellent, thanks Klaus!
Cheers,
Eoghan.
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sun Apr 06, 2014 5:26 pm
by jacque
The engine has worked this way since the beginning and I don't expect it to change. I rely on the behavior all the time and I think a lot of code would break if it didn't. The original author of MetaCard said he did it on purpose in the interest of efficiency.
Re: Count on 'if' control structure's lazy evaluation?
Posted: Sun Apr 06, 2014 6:28 pm
by Thierry
Hello,
In fact, this is not really a Lazy evaluation,
but a short circuit boolean evaluation,
which most of modern languages have!
( Ada, Java have 2 differents operators to use or not this feature because
of some side-effect )
http://en.wikipedia.org/wiki/Short-circuit_evaluation
Regards,
Thierry
Re: Count on 'if' control structure's lazy evaluation?
Posted: Mon Apr 07, 2014 2:46 pm
by Eoghan
Thanks for your comments Jacqueline & Thierry, very useful to know.
Cheers,
Eoghan.