Assignment, Equality and Identity

Dylan uses all three of the “equals” operators found in C and Pascal, albeit in a different fashion. The Pascal assignment operator, :=, rebinds Dylan variable names to new values. The Pascal equality operator, = , tests for equality in Dylan and also appears in some language constructs such as let. (Two Dylan objects are equal, generally, if they belong to the same class and have equal substructure.)

The C equality operator, ==, acts as the identity operator in Dylan. Two variables are identical if and only if they are bound to the exact same object. For example, the following three expressions mean roughly the same thing:

(a == b)   // in Dylan
(&a == &b) // in C or C++
(@a = @b) // in Pascal

The following piece of source code demonstrates all three operators in actual use.

let car1 = make(<car>);
let car2 = make(<car>);
let car3 = car2;

car2 = car3;	// #t
car1 = car2;	// ??? (see below)
car2 == car3;	// #t
car1 == car2;	// #f

car2 := car1;	// rebind
car1 == car2;	// #t

let x = 2;
let y = 2;

x = y;			// #t
x == y;			// #t (only one 2!)

Two of the examples merit further explanation. First, we don't know whether car1 = car2, because we don't know if make creates each car with the same serial number, driver and other information as previous cars. If and only if none of those values differ, then car1 equals car2. Second, x == y because every variable bound to a given number refers to the exact same instance of that number, at least from the programmer's perspective. (The compiler will normally do something more useful and efficient when generating the actual machine code.) Strings behave in a fashion different from numbers— instances of strings are stored separately, and two equal strings are not necessarily the same string.