Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 ->
 			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