English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Closed Objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1996-09-13 (07:59)
From: Andrew Conway <arc@s...>
Subject: Closed Objects

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

(* 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 

(* 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)

(* 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 

class go n as self =
    inherit runaround ()
    val storen = n
    method null = false
    method next = storen
    method myself = (self :> runaround)

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;;