caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Open datatypes
@ 2011-07-14 13:38 lpw25
  2011-07-14 14:48 ` Markus Mottl
  0 siblings, 1 reply; 5+ messages in thread
From: lpw25 @ 2011-07-14 13:38 UTC (permalink / raw)
  To: caml-list


Hi all,

I was wondering whether there was any particular reason why OCaml 
doesn't allow the user to create open extensible datatypes like exn.

I know that something similar can be created using local exceptions:

module T = struct
  type t = exn

  type 'a tvariant = (('a -> t), (t -> 'a option))

  let createVariant (type s) () =
    let module M = struct exception E of s end in
      (fun x -> M.E x), (function M.E x -> Some x | _ -> None)

  let mkTVariant ((cnstr, _) :	'a tvariant) (x: 'a) = cnstr x

  let matchTVariant ((_, destr) : 'a tvariant) (xt: t) = destr xt
end 

but it isn't very neat, and it seems that it would not be that difficult to 
allow the user to declare types with the same properties as exn.

Thanks,

Leo

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

end of thread, other threads:[~2011-07-14 16:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-14 13:38 [Caml-list] Open datatypes lpw25
2011-07-14 14:48 ` Markus Mottl
2011-07-14 15:10   ` Gerd Stolpmann
2011-07-14 16:05     ` Leo P White
2011-07-14 15:15   ` 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).