Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Variant type with 'a type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-06-10 (11:27)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Variant type with 'a type?

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 ->
 				f x
 			| e -> Exception e

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

 	let return x = Result x;;

 	let fail s = Exception (Failure s)

Welcome to the wonderful world of monads.