Count on 'if' control structure's lazy evaluation?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Eoghan
Posts: 9
Joined: Tue Dec 17, 2013 3:38 pm

Count on 'if' control structure's lazy evaluation?

Post by Eoghan » Sat Apr 05, 2014 5:49 pm

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
LazyEval_Test.livecode.zip
Stack showing lazy evaluation
(1.21 KiB) Downloaded 194 times
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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count on 'if' control structure's lazy evaluation?

Post by Klaus » Sat Apr 05, 2014 5:55 pm

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 :D


Best

Klaus

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count on 'if' control structure's lazy evaluation?

Post by Klaus » Sat Apr 05, 2014 5:57 pm

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
:D

Eoghan
Posts: 9
Joined: Tue Dec 17, 2013 3:38 pm

Re: Count on 'if' control structure's lazy evaluation?

Post by Eoghan » Sat Apr 05, 2014 6:11 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count on 'if' control structure's lazy evaluation?

Post by Klaus » Sat Apr 05, 2014 6:58 pm

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 :D


Best

Klaus

Eoghan
Posts: 9
Joined: Tue Dec 17, 2013 3:38 pm

Re: Count on 'if' control structure's lazy evaluation?

Post by Eoghan » Sat Apr 05, 2014 7:43 pm

Excellent, thanks Klaus!

Cheers,
Eoghan.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Count on 'if' control structure's lazy evaluation?

Post by jacque » Sun Apr 06, 2014 5:26 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Count on 'if' control structure's lazy evaluation?

Post by Thierry » Sun Apr 06, 2014 6:28 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Eoghan
Posts: 9
Joined: Tue Dec 17, 2013 3:38 pm

Re: Count on 'if' control structure's lazy evaluation?

Post by Eoghan » Mon Apr 07, 2014 2:46 pm

Thanks for your comments Jacqueline & Thierry, very useful to know.

Cheers,
Eoghan.

Post Reply