caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Problem with Arg.Tuple
@ 2012-03-13 10:49 Gabriel Cardoso
  2012-03-13 11:21 ` Samuel Hornus
       [not found] ` <070E17B9-F989-4184-A702-7D323710F52B@inria.fr>
  0 siblings, 2 replies; 3+ messages in thread
From: Gabriel Cardoso @ 2012-03-13 10:49 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 705 bytes --]

Dear list,

I have empty arguments when using Arg.Tuple with references. This
minimalist example illustrates my problem :

let f a b =
  print_endline a;
  print_endline b

let s = ref ""

let speclist = [(
  "-a",
  Arg.Tuple [
    Arg.Set_string s;
    Arg.String (f !s) (* !s is empty !!! *)
    (* Arg.String (fun ss -> f !s ss) (\* Works just fine ... why ? *\) *)
  ],
  "doc"
)]

let _ = Arg.parse speclist (fun s -> ()) "usage"

--------------------
$ ocamlc test.ml
$ /a.out -a x y

y
------------------------

When uncommenting the line 12 :
------------------------
$ ocamlc test.ml
$ ./a.out -a x y
x
y
-------------------------

Any idea what am I doing wrong ?

Thanks in advance !

Gabriel

[-- Attachment #2: Type: text/html, Size: 1301 bytes --]

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

* Re: [Caml-list] Problem with Arg.Tuple
  2012-03-13 10:49 [Caml-list] Problem with Arg.Tuple Gabriel Cardoso
@ 2012-03-13 11:21 ` Samuel Hornus
       [not found] ` <070E17B9-F989-4184-A702-7D323710F52B@inria.fr>
  1 sibling, 0 replies; 3+ messages in thread
From: Samuel Hornus @ 2012-03-13 11:21 UTC (permalink / raw)
  To: Gabriel Cardoso


On Mar13, 2012, at 11:49 , Gabriel Cardoso wrote:

> Dear list, 
> 
> I have empty arguments when using Arg.Tuple with references. This minimalist example illustrates my problem :
> 
> let f a b =
>  print_endline a;
>  print_endline b
> 
> let s = ref ""
> 
> let speclist = [(
>  "-a",
>  Arg.Tuple [
>    Arg.Set_string s;
>    Arg.String (f !s) (* !s is empty !!! *)

Here, the value of !s is empty when the (f !s) is evaluated…
you shoud pass the whole reference as argument :
Arg.String(f s)
and 
let f a b = print_endline !a; print_endline b

>    (* Arg.String (fun ss -> f !s ss) (\* Works just fine ... why ? *\) *)

why ? because the !s is evaluated only later, when the anonymous function (fun ss ->…) is applied.
-- 
Sam

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

* Re: [Caml-list] Problem with Arg.Tuple
       [not found] ` <070E17B9-F989-4184-A702-7D323710F52B@inria.fr>
@ 2012-03-13 12:23   ` Gabriel Cardoso
  0 siblings, 0 replies; 3+ messages in thread
From: Gabriel Cardoso @ 2012-03-13 12:23 UTC (permalink / raw)
  To: Samuel Hornus; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 919 bytes --]

Thanks, I get it now !

Cheers

Gabriel

2012/3/13 Samuel Hornus <samuel.hornus@inria.fr>

>
> On Mar13, 2012, at 11:49 , Gabriel Cardoso wrote:
>
> > Dear list,
> >
> > I have empty arguments when using Arg.Tuple with references. This
> minimalist example illustrates my problem :
> >
> > let f a b =
> >   print_endline a;
> >   print_endline b
> >
> > let s = ref ""
> >
> > let speclist = [(
> >   "-a",
> >   Arg.Tuple [
> >     Arg.Set_string s;
> >     Arg.String (f !s) (* !s is empty !!! *)
>
> Here, the value of !s is empty when the (f !s) is evaluated…
> you shoud pass the whole reference as argument :
> Arg.String(f s)
> and
> let f a b = print_endline !a; print_endline b
>
> >     (* Arg.String (fun ss -> f !s ss) (\* Works just fine ... why ? *\)
> *)
>
> why ? because the !s here is evaluated only later, whenthe anonymous
> function is applied.
> --
> Sam

[-- Attachment #2: Type: text/html, Size: 1425 bytes --]

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

end of thread, other threads:[~2012-03-13 12:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-13 10:49 [Caml-list] Problem with Arg.Tuple Gabriel Cardoso
2012-03-13 11:21 ` Samuel Hornus
     [not found] ` <070E17B9-F989-4184-A702-7D323710F52B@inria.fr>
2012-03-13 12:23   ` Gabriel Cardoso

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