* self in classes
@ 1999-02-06 14:09 Николай Ципанов
1999-02-08 10:34 ` Didier Remy
1999-02-08 12:21 ` Jerome Vouillon
0 siblings, 2 replies; 3+ messages in thread
From: Николай Ципанов @ 1999-02-06 14:09 UTC (permalink / raw)
To: Caml list
Hi!
Are there any ways to call the methods of other objects with self as an
argument ?
I mean situation like that:
class foo = object (self)
val observers : bar list = []
method register =
Array.iter (function o -> o#register self ) observers
.... ^^^^^^^^^^^^^^^^^^
end
class bar = object
method register ob =
(* add something *)
end
When i'm trying to write something like that ocamlc says me that
self canot escape of it's definition
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: self in classes
1999-02-06 14:09 self in classes Николай Ципанов
@ 1999-02-08 10:34 ` Didier Remy
1999-02-08 12:21 ` Jerome Vouillon
1 sibling, 0 replies; 3+ messages in thread
From: Didier Remy @ 1999-02-08 10:34 UTC (permalink / raw)
To: îÉËÏÌÁÊ
ãÉÐÁÎÏ×
Cc: Caml list
> Are there any ways to call the methods of other objects with self as an
> argument ?
> I mean situation like that:
> class foo = object (self)
> val observers : bar list = []
> method register =
> Array.iter (function o -> o#register self ) observers
> .... ^^^^^^^^^^^^^^^^^^
There is a similar example (the subject/observer pattern) in
the advanced tutorial http://caml.inria.fr/ocaml/htmlman/node5.html
-Didier
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: self in classes
1999-02-06 14:09 self in classes Николай Ципанов
1999-02-08 10:34 ` Didier Remy
@ 1999-02-08 12:21 ` Jerome Vouillon
1 sibling, 0 replies; 3+ messages in thread
From: Jerome Vouillon @ 1999-02-08 12:21 UTC (permalink / raw)
To: ??????? ???????, Caml list
On Sat, Feb 06, 1999 at 05:09:54PM +0300, ??????? ??????? wrote:
> Are there any ways to call the methods of other objects with self as an
> argument ?
[...]
> When i'm trying to write something like that ocamlc says me that
> self canot escape of it's definition
If I understand right, you're trying to mutally define two classes
this way:
class foo = object (self)
val observers : bar list = []
method register =
List.iter (function o -> o#register self) observers
end and bar = object
method register _ = ()
end;;
This fails, because the inferred type for the method register of class
bar would be 'a -> unit, where 'a is the type of self in class
foo. But this type should not appear outside class foo.
There are two ways to solve the problem. The first is to let the
method register of class bar have a more general type, and break the
recursion between the two classes :
class ['a] bar = object
method register (_ : 'a) = ()
end;;
class foo = object (self)
val observers : 'a bar list = []
method register =
List.iter (function o -> o#register self ) observers
end;;
The second way is to add a coercion of self to some fixed type:
class foo = object (self)
val observers : bar list = []
method register =
List.iter (function o -> o#register (self :> < >) ) observers
end and bar = object
method register _ = ()
end;;
-- Jérôme
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1999-02-08 14:18 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-06 14:09 self in classes Николай Ципанов
1999-02-08 10:34 ` Didier Remy
1999-02-08 12:21 ` Jerome Vouillon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox