Re: Booleans in positional parameter sub routines [was; Can someone explain this?}
Re: Booleans in positional parameter sub routines [was; Can someone explain this?}
- Subject: Re: Booleans in positional parameter sub routines [was; Can someone explain this?}
- From: Cal <email@hidden>
- Date: Mon, 18 Dec 2000 00:10:50 -0500
Sun Real <email@hidden> wrote:
Trying to use the above, I came across this:
to doSomething with noise -- works
if noise then beep
end doSomething
to doSomething with noise -- execution error: "The variable noise is
not defined."
set a to 1
if noise then beep
end doSomething
to doSomething with noise -- works
noise
set a to 1
if noise then beep
end doSomething
And:
Testing here suggests that 'Set2be' is compiling as a positional
parameter sub-routine call with just a boolean parameter:
Set2be with to
set not2be to false
to Set2be with to
beep
end Set2be
This runs here. I don't know why the compiler allows the keyword 'to' to
be passed as a boolean param (it didn't appear to be possible to use it),
but if a non-reserved word is used, it becomes a switch:
Set2be with too
set not2be to false
to Set2be with too
if too then
beep
else
beep 2
end if
end Set2be
I highly recommend _against_ using "with" as the keyword for a
keyword parameter name. If the value passed in is anything other
than a boolean, you won't be able to get the value. Also, you'll get
really confused because either "with" or "without" work the same in
the handler declaration, regardless of how the call is written. For
reasons of ambiguity, I'd avoid using this construct on handler
declarations.
I also recommend against using "to" as a formal parmeter value. The
example above won't work with "to":
to Set2be with to
if to then <-- illegal syntax
beep
else
beep 2
end if
end Set2be
You're much better off using keywords other than "with" and "without"
on the declarations. You can check the class of the parameter to see
whether you've got a boolean, if you're expecting a boolean.
Cal