Re: operational semantics of = for objects

From: Jacques GARRIGUE (garrigue@kurims.kyoto-u.ac.jp)
Date: Fri Mar 27 1998 - 13:14:25 MET


To: Sorin.Stratulat@loria.fr
Subject: Re: operational semantics of = for objects
In-Reply-To: Your message of "Thu, 26 Mar 1998 18:32:49 +0100"
        <351A9141.3D52@loria.fr>
Message-Id: <19980327211425D.garrigue@kurims.kyoto-u.ac.jp>
Date: Fri, 27 Mar 1998 21:14:25 +0900
From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>

From: Sorin Stratulat <Sorin.Stratulat@loria.fr>

> 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
oo.ml 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
    equal.

        Jacques

---------------------------------------------------------------------------
Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp
                <A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



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