Version française
Home     About     Download     Resources     Contact us    
Browse thread
operational semantics of = for objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques GARRIGUE <garrigue@k...>
Subject: Re: operational semantics of = for objects
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>