Re: operational semantics of = for objects

From: Jacques GARRIGUE (
Date: Fri Mar 27 1998 - 13:14:25 MET

Subject: Re: operational semantics of = for objects
In-Reply-To: Your message of "Thu, 26 Mar 1998 18:32:49 +0100"
Message-Id: <>
Date: Fri, 27 Mar 1998 21:14:25 +0900
From: Jacques GARRIGUE <>

From: Sorin Stratulat <>

> After comparing the results of the code (see below) executed on
> different versions of OCAML (1.05 and 1.07), I would like to know if
> anybody can explain me how the equality operator works for objects (if
> it is possible in both versions). As I understood from a previous
> message, overloading the operators is not supported yet.

The fastest way to understand what has changed is to have a look at in the standard library. In 1.07, all objects have an oid,
unique to them, like in many object-oriented languages, and as a
result structural equality (=) will always give them different.

That is

        let o1 = new c ()
        and o2 = new c ()
        in o1 = o2;;
        - : bool = false

        let o1 = new c () in
        let o2 = Oo.copy o1 in
        o1 = o2;;
        - : bool = false

        let o1 = new c () in
        let o2 = o1 in
        o1 = o2;;
        - : bool = true

What you must understand is that the last example does not define two
objects but only two names for the same object, and this is why they
are equal.

In 1.05 there was no oid (I suppose), so two objects would be equal
when their instance variables are equal. This may look nice, but there
are several drawbacks:
  * if there are functions stored in instance variables, trying to
    compare two objects would raise an exception.
  * even if instance variables are equal, there are still ways to
    distinguish two objects (particularly if they have mutable
    instance variables), so you cannot consider them as rigorously


Jacques Garrigue Kyoto University garrigue at
                <A HREF=>JG</A>

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:14 MET