caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [ANN] ocamlopen 1.0.2
@ 2012-04-11 21:13 Leo P White
  2012-04-12 12:56 ` Gabriel Scherer
  0 siblings, 1 reply; 9+ messages in thread
From: Leo P White @ 2012-04-11 21:13 UTC (permalink / raw)
  To: caml

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-04-17 10:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-11 21:13 [Caml-list] [ANN] ocamlopen 1.0.2 Leo P White
2012-04-12 12:56 ` Gabriel Scherer
2012-04-12 14:03   ` Alain Frisch
2012-04-12 14:16     ` Philippe Veber
2012-04-12 14:29       ` Alain Frisch
2012-04-17 10:02         ` Philippe Veber
2012-04-12 17:07   ` Leo P White
2012-04-12 17:21     ` Alain Frisch
2012-04-12 20:31       ` Leo P White

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).