caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Variant type with 'a type?
@ 2008-06-10  2:29 Robert Fischer
  2008-06-10  2:32 ` [Caml-list] " Robert Fischer
  2008-06-10 12:01 ` Brian Hurt
  0 siblings, 2 replies; 3+ messages in thread
From: Robert Fischer @ 2008-06-10  2:29 UTC (permalink / raw)
  To: caml-list

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".

~~ Robert.


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

* Re: [Caml-list] Variant type with 'a type?
  2008-06-10  2:29 Variant type with 'a type? Robert Fischer
@ 2008-06-10  2:32 ` Robert Fischer
  2008-06-10 12:01 ` Brian Hurt
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Fischer @ 2008-06-10  2:32 UTC (permalink / raw)
  To: caml-list

Figured it out -- "type 'a maybe = Result of 'a | Exception of exn"

Putting the tick-a before the "maybe" was nonintuitive to me.

~~ Robert.

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".
> 
> ~~ Robert.
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 


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

* Re: [Caml-list] Variant type with 'a type?
  2008-06-10  2:29 Variant type with 'a type? Robert Fischer
  2008-06-10  2:32 ` [Caml-list] " Robert Fischer
@ 2008-06-10 12:01 ` Brian Hurt
  1 sibling, 0 replies; 3+ messages in thread
From: Brian Hurt @ 2008-06-10 12:01 UTC (permalink / raw)
  To: Robert Fischer; +Cc: caml-list



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


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

end of thread, other threads:[~2008-06-10 11:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-10  2:29 Variant type with 'a type? Robert Fischer
2008-06-10  2:32 ` [Caml-list] " Robert Fischer
2008-06-10 12:01 ` Brian Hurt

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).