Version française
Home     About     Download     Resources     Contact us    
Browse thread
try .. finally using new camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: tab@s...
Subject: Re: [Caml-list] try .. finally using new camlp4
On Sun, Jul 08, 2007 at 01:14:41AM +0100, Jon Harrop wrote:
> I'm just getting my toe wet with the new camlp4 and I can't get this simple 
> syntax extension to work. AFAICT, the following code:

A bit OT, but I found it quite easy to actually hack ocaml source
directly to do the right thing for try finally. My modification seems to
works correctly, without having any deep knowledge of ocaml internal
compiler code. 

>   EXTEND Gram
>     expr: LEVEL "top"
>     [[ "try"; f=expr; "finally"; g=expr ->
> 	 <:expr<
> 	   ((function
> 	     | `Val v, g -> g(); v
> 	     | `Exn e, g -> g(); raise e)
> 	      ((try `Val($f$) with e -> `Exn e), (fun () -> $g$)))
> 	 >>]];
>   END

I don't know camlp4, but why don't you use a more straightforward
(let r = try f() with e -> g(); raise e in g(); r) construct
instead of wrapping/unwrapping the thing into polymorphic variant ?

Cheers,
-- 
Vincent Hanquez