Re: Non-executed repeat loop robs variable of value/definition
Re: Non-executed repeat loop robs variable of value/definition
- Subject: Re: Non-executed repeat loop robs variable of value/definition
- From: Matt Neuburg <email@hidden>
- Date: Sun, 28 Aug 2005 10:04:50 -0700
On Sun, 28 Aug 2005 16:21:56 +0100, kai <email@hidden> said:
>set n to 1
>if false then repeat with n from 1 to 2
>end repeat
>n
This should not even compile. You have an "end repeat" without a matching
"repeat". However, let's ignore that! :) The same phenomenon happens if we
rewrite normally:
set n to 1
if false then
repeat with n from 1 to 2
end repeat
end if
n
The problem is that you are redefining a global as a local. This is not
illegal but it cuts off your access to the global (see my book for copious
and gory details). The repeat is not executed but it is still parsed by the
compiler and still performs the local declaration implicitly. To solve the
problem, be good and Declare Your Variables!
local n
set n to 1
if false then
repeat with n from 1 to 2
end repeat
end if
n -- 1
Alternatively, specify that you mean the global:
set n to 1
if false then
repeat with n from 1 to 2
end repeat
end if
my n -- 1
What I find interesting is what happens if you change false to true, i.e. it
works fine. I guess if the repeat block actually executes, then by that
time, a global n exists and is in scope and the repeat block uses it
instead. So I'd say what you've found is a bug, because the two really
should behave the same way. But then, this whole thing about implicit
declaration of variables is one massive mistake in my opinion anyway: there
was never a hope in Hades that every case would get covered coherently, and
you've neatly poked a hole in it. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden