Tricks with equality

1. Comparing things of the same type

Director has a few idiosyncrasies when comparing things depending on what type the things are (especially when they are different types!). Here's a list of how some comparisons work:

Strings

Comparing strings is case-insensitive ("foo" = "Foo") unless the strings are inside a list (["foo"] <>["Foo"]).

Symbols

Symbols don 't really have a case (they have an integer equivalent which seems to be used for comparisons). Therefore, #foo = #FOO and [#foo] = [#FOO]. Also note that this lack of case makes symbols more flexible than strings for use as properties in PropLists.

Numbers

Some obscure types such as NAN and NULL have their own rules (best to convert them to strings for any comparison):

a = sqrt(-1)

b = sqrt(-1)

put a,b

-- NAN NAN

put a = b

-- 0

a = power(10, 1000)

b = power(10, 1000)

put a,b

-- INF INF

put a = b

-- 0

Objects (lists, images, etc)

When comparing objects, Director will often try and help by comparing values or properties of the object. For example, to determine whether image1 = image2 director will do a pixel-by-pixel comparison (not compare whether the object or memory address of the object(s) referenced are the same). With lists, Director compares the values (see note above on how text values in a list are compared). Note that with property Lists, the property seems to be ignored. What is compare is the value at each index in the list:

a = [#Name: "Nada", #Pass: "Chew bubblegum"]

b = [#Pass: "Nada", #Name: "Chew bubblegum"]

put a = b

-- 1

c = [#Pass: "Chew bubblegum", #Name: "Nada"]

put a=c

-- 0

Comparing script instances seems involve an comparison of memory addresses (ie whether the two variables point to the same object, or to different objects).

2. Comparing things of the different types

When comparing things of different types, Director usually tries to be helpful and does some type casting in the background

a = "1" 

b = 1

put a = b

-- 1

Sometimes it can be too helpful:

a = VOID

b = 0

put a = b

-- 1

In any event, if you want to check whether to thing things are identical, then its best to use a function like

on equal (a, b)

  -- check the ilk, and then wrap in lists 

  -- for case-sensitivity of strings

  if a.ilk = b.ilk then return ([a] = [b])

  else return 0

end

Though note that this will still treat false and 0 as identical (the data type of #boolean isn't recognised by ilk).  

First published 31/03/2006