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: Jeremy Yallop <jeremy.yallop@e...>
Subject: Re: [Caml-list] try .. finally using new camlp4
Vincent Hanquez wrote:
>>   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 ?

Jon's version avoids name capture.  If the user writes

    let r = 1 in
      try
        2
      finally
        print_endline (string_of_int r)

then your version (modulo the unit arguments) expands into

    let r = 1 in
       let r = try 2
               with e ->
                  print_endline (string_of_int r);
                  raise e in
            print_endline (string_of_int r);
            r

which prints "2" instead of "1".

Jeremy.