* Closed Objects
@ 1996-09-12 19:22 Andrew Conway
1996-09-13 12:31 ` Jerome Vouillon
0 siblings, 1 reply; 2+ messages in thread
From: Andrew Conway @ 1996-09-12 19:22 UTC (permalink / raw)
To: caml-list
Dear Caml Implementors,
I was wondering if you could give some comments upon the interactions
of classes returning self, type coercions, inheritance and closedness.
In particular, I do not understand the rules on closedness.
I was trying to make a "list like" class, and found the following
effects:
(* Base list class. Has no "head", but I am not interested about
storing data in it yet. This works in ocaml 1.00 and 1.01.
The "myself" method will usually be "self", but occasionally
(in some subclasses) it will refer to something else.
*)
class virtual runaround () as self : 'a =
virtual null : bool
virtual next : runaround
virtual myself : runaround
end;;
(* Add a method. This DOESN'T compile in ocaml 1.01, but
did compile in ocaml 1.00. The error message says that it
is closed, but not marked closed.
I don't want it to be closed. Is it impossible to return "self"
in a non-closed class?
*)
class virtual run2 () as self =
inherit runaround ()
method myself = (self :> runaround)
end;;
(* the sort of way things would be used (works without "myself") *)
class stop () as self =
inherit run2 ()
method null = true
method next = invalid_arg "stop"
method junk = 6
end;;
class go n as self =
inherit runaround ()
val storen = n
method null = false
method next = storen
method myself = (self :> runaround)
end;;
let s = new stop ();;
let ss = (s :> runaround) ;;
let g = new go ss;;
let gg = new go g;;
let rec laps x =
if x#null then 0 else 1 + laps x#next
;;
laps g;;
laps gg;;
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Closed Objects
1996-09-12 19:22 Closed Objects Andrew Conway
@ 1996-09-13 12:31 ` Jerome Vouillon
0 siblings, 0 replies; 2+ messages in thread
From: Jerome Vouillon @ 1996-09-13 12:31 UTC (permalink / raw)
To: Andrew Conway; +Cc: caml-list
> class virtual runaround () as self : 'a =
> virtual null : bool
> virtual next : runaround
> virtual myself : runaround
> end;;
>
> (* Add a method. This DOESN'T compile in ocaml 1.01, but
> did compile in ocaml 1.00. The error message says that it
> is closed, but not marked closed.
>
> I don't want it to be closed. Is it impossible to return "self"
> in a non-closed class?
>
> *)
>
> class virtual run2 () as self =
> inherit runaround ()
> method myself = (self :> runaround)
> end;;
Coercion operator ( e :> t ) is not always general enough. What it does is
guess a type t' subtype of t independant of expression e, and then checks
the type of expression e is an instance of type t'. For instance, if
t = <x : int>, then t' = <x : int; ..>. In this case, any subtype of t is
an instance of type t'. But this is not always the case. Consider type
t = <x : 'a> as 'a. The types <x : 'b; y : int> as 'b and
<x : t; y : int> are both subtypes of this type, but they are not
instances of a common subtype of type t. So, one have to use a heuristic
to find a type t' that works in most cases (I have changed the algorithm
between ocaml 1.00 and ocaml 1.01, that's why your example compiles with
the first version, but not with the second version).
Let us come back to your example. You can see here the type t' guessed:
#fun x -> (x :> runaround);;
- : (< null : bool; next : 'a; myself : 'a; .. > as 'a) -> runaround = <fun>
\_________________________________________________/
t'
If you unify type t' with the type of self in class run2, you will get
type runaround. Indeed, method myself of self has type runaround, so
runaround = 'a (unification of types of myself)
= type of self
The type runaround is closed, hence the error message.
The workaround is to use the more general coercion operator ( e : t' :> t )
when the other one fails.
#class virtual run2 () as self =
# inherit runaround ()
# method myself = (self : #runaround :> runaround)
#end;;
class virtual run2 (unit) =
method myself : runaround
virtual null : bool
virtual next : runaround
end
Jerome
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1996-09-13 16:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-12 19:22 Closed Objects Andrew Conway
1996-09-13 12:31 ` Jerome Vouillon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox