local and do <commands> - what NOT to do

Kay C Lan lan.kc.macmail at gmail.com
Thu Feb 18 03:03:07 EST 2016


The Dictionary entry for 'local' has this Example:

-- To make a numbered list of variables:
repeat with x=1 to 20
  do "local tVar_" & x & "; put empty into tVar_" & x
end repeat

Which 'apparently' runs fine if you copy and paste it into the msg box. For
the purposes of this discussion and so we can see what's going on I'm going
to remix that to:

repeat with x=1 to 20
  do "local tVar_" & x
  do "put x*x into tVar_" & x
end repeat

As Hermann pointed out in a previous thread, if you use Strict Compilation
mode, to access any of those variables you can't:

put tVar_5 into msg

because when you apply the changes you get an error because LC doesn't know
tVar_5 has been declared in <do>. To work around this EVERY SINGLE TIME you
have to refer to ANY variable created by <do> you have to hide it in
another <do>:

do "put tVar_5 into msg" --making <do> even slower (time to learn about
arrays;-)

BUT, assuming you are severely allergic to arrays and simply must use <do>,
then what is the point of the <local> statement? If you accidentally in the
final statement wrote:

do "put tBar_5 into msg"  --Bar not Var

Strict Mode will NOT pick this up as all <do> variables are basically
hidden.

Moving right along, for those Slack Moders, if you run the same statements:

repeat with x=1 to 20
  do "local tVar_" & x
  do "put x*x into tVar_" & x
end repeat
put tVar_5 into msg  --not hidden in <do>

For reasons I do NOT understand, you get a runtime error:

line: do "local tVar_" & x
hint: local tVar_5

If you remove/comment the line: do "local tVar_" & x

everything will then run fine - reinforcing your decision to choose Slack
Mode because declaring vars is so 3GL ;-)

So long story short; contrary to the example, it would seem to be
counter-productive to use <local> within a <do> statement.



More information about the use-livecode mailing list