Is zero the same as empty?
J. Landman Gay
jacque at hyperactivesw.com
Mon Oct 27 14:24:56 EDT 2014
On 10/27/2014, 11:20 AM, Graham Samuel wrote:
> I found that if a parameter of a handler or function, or indeed a
> local variable, (say t1) has never had a value placed in it, then
>
> t1 = 0
>
> is true. I was surprised by this, since I thought all unassigned
> variables were rendered empty by the engine, and that empty and zero
> were distinct.
>
> But in the same script
>
> t1 = empty
>
> is also true. Logically then, ‘empty’ is the same as ‘0’. However the
> statement
>
> 0 = empty
>
> evaluates to false. Furthermore if I make sure t1 is empty by
>
> put empty into t1
>
> then
>
> 0 = t1
>
> yields false.
>
> Am I going crazy? This issue matters to me as I am comparing numeric
> characters with a variable which is sometimes unassigned. I see that
> I must always assign a value to the variable, even it that value is
> empty!
>
> Is this any kind of a bug, or is it just slightly strange behaviour
> on the part of the engine?
Empty is sort of a Schrödinger variable and changes its state depending
on how you look at it. If it is used in a numeric calculation it resoves
to zero. Otherwise it resolves to empty. You don't need to do anything
in particular, the engine handles that. In your tests, neither t1= empty
and t1= 0 force an evaluation, so both are true. But zero and empty are
not equivalent (one is a numeric value, one has no value at all,) so
that evaluation is false. When you specifically put empty into t1, then
it no longer equals zero.
In a handler that calculates empty variables in a math operation, you
will always get the right answer. If you only need to see if the
variable has a particular value, something like this always resolves
correctly:
if t1 < 1 then... -- true for both zero and empty
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list