Variable uninitialized variable values (or unexpected declarations)

Bob Rasmussen brasmussen at earthlink.net
Sun Mar 9 03:30:01 EST 2003


I found the following to be rather surprising.

Set the script of a button to the following:


    on mouseUp

      switch 1

      case 1
        put "var =" && quote & var & quote
        break

      case 2
        put "whatever" into var
        break

      end switch

    end mouseUp


Press the button and the message box will say

    var = "var"

However, reverse the cases in the switch so case 2 statements comes first, press
the button again, and the message box will now say

   var = ""

The behavior changes, even though case 2 is never executed in either version of
the script.  Similar behavior can be demonstrated with other control structures,
but this example seems the most disturbing.

A little experimentation resulted in my learning the following:

1.  The uninitialized value of an undeclared local variable is its name.  I
already knew that, though I couldn't find it in the documentation.  Did I miss
it?

2.  Apparently, any line of the form "put ... into|before|after ... var" seems
to be equivalent to "local var", as far as declaring var is concerned, *even if
that line of the script is never executed*.  Declarations seem to depend only on
order of appearance in the script text.  I guess that means they're not really
true commands, since they have nothing to do with execution.  I couldn't find a
description of this in the documentation.  Did I miss it?

3.  Moreover, this doesn't seem right to me.  Having the uninitialized value of
a variable vary in this way is a little disconcerting, and if I had a choice,
I'd prefer it always to be empty -- the default declaration value.  That way,
jugging the order of cases, as above, and similar changes would have no effect.
That seems much more reasonable.  Presumably though, the present behavior has
been around for a very long time.  Is there a reason for it being this way?

RR





More information about the use-livecode mailing list