caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo P White <lpw25@cam.ac.uk>
To: caml <caml-list@inria.fr>
Subject: [Caml-list] [ANN] ocamlopen 1.0.2
Date: 11 Apr 2012 22:13:09 +0100	[thread overview]
Message-ID: <Prayer.1.3.4.1204112213090.4991@hermes-2.csi.cam.ac.uk> (raw)

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


             reply	other threads:[~2012-04-11 21:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-11 21:13 Leo P White [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Prayer.1.3.4.1204112213090.4991@hermes-2.csi.cam.ac.uk \
    --to=lpw25@cam.ac.uk \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).