Hi,
If anyone is interested, I have written a new version of my patch to
add open extensible types to OCaml. It is available at:
https://sites.google.com/site/ocamlopen/
The only new feature is allowing variant declarations to be made
extensible. This allows declarations like:
open type foo = A | B of int | ..
Having implemented it, I think that the extension might well be better
off without this feature, so I am also releasing another version of
the patch without it.
I have also written a much better example of how open types and GADTs
might be used. It basically shows how classes can be created that
permit a kind of nominative down-casting. I have included it below.
Finally, I have also added a feature request to Mantis if anyone would
like to comment.
http://caml.inria.fr/mantis/view.php?id=5584
Regards,
Leo
--------------------------------
open type 'a class_name
exception Bad_cast
class type castable =
object
method cast: 'a.'a class_name -> 'a
end
(* Lets create a castable class with a name*)
class type foo_t =
object
inherit castable
method foo: string
end
extend 'a class_name with Foo: foo_t class_name
class foo: foo_t =
object(self)
method cast: type a. a class_name -> a =
function
Foo -> (self : #foo_t :> foo_t)
| _ -> ((raise Bad_cast) : a)
method foo = "foo"
end
(* Now we can create a subclass of foo *)
class type bar_t =
object
inherit foo
method bar: string
end
extend 'a class_name with Bar: bar_t class_name
class bar: bar_t =
object(self)
inherit foo as super
method cast: type a. a class_name -> a =
function
Bar -> (self : #bar_t :> bar_t)
| other -> super#cast other
method bar = "bar"
end
(* Now lets create a mutable list of castable objects *)
let clist :castable list ref = ref []
let push_castable (c: #castable) =
clist := (c :> castable) :: !clist
let pop_castable () =
match !clist with
c :: rest ->
clist := rest;
c
| [] -> raise Not_found;;
(* We can add foos and bars to this list, and retrive them *)
push_castable (new foo);;
push_castable (new bar);;
push_castable (new foo);;
let c1: castable = pop_castable ()
let c2: castable = pop_castable ()
let c3: castable = pop_castable ()
(* We can also downcast these values to foos and bars *)
let f1: foo = c1#cast Foo
let f2: foo = c2#cast Foo
let f3: foo = c3#cast Foo
let b2: bar = c2#cast Bar
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs