caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Robert Fischer <robert.fischer@smokejumperit.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Variant type with 'a type?
Date: Tue, 10 Jun 2008 08:01:57 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0806100757100.21480@localhost> (raw)
In-Reply-To: <484DE703.50503@smokejumperit.com>



On Mon, 9 Jun 2008, Robert Fischer wrote:

> Is it possible to do a variant type along the lines of "type try = Result of 'a | Exception of exn"?
>   When I try that, I get an error saying "Unbound type parameter 'a".

1) try is a key
2) paramaterize the type.

so you can do:
type 'a try_t =
 	| Result of 'a
 	| Exception of exn

And while you're digging at this spot, let me point out that you might 
want to define the module like this:

module Try = struct
 	let bind m f =
 		match m with
 		| Exception e -> Exception e
 		| Result x ->
 			try
 				f x
 			with
 			| e -> Exception e
 	;;

 	let ( >>= ) m f = bind m f;;

 	let return x = Result x;;

 	let fail s = Exception (Failure s)
end;;

Welcome to the wonderful world of monads.

Brian


      parent reply	other threads:[~2008-06-10 11:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-10  2:29 Robert Fischer
2008-06-10  2:32 ` [Caml-list] " Robert Fischer
2008-06-10 12:01 ` Brian Hurt [this message]

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=Pine.LNX.4.64.0806100757100.21480@localhost \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=robert.fischer@smokejumperit.com \
    /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).