Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: small code problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <Damien.Doligez@i...>
Subject: Re: small code problem
>From: "<Brad Knotwell" <knotwell@f5.com>

>let usage = lazy (Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1);;
>let filename =
>        try
>           Sys.argv.(1)
>        with Invalid_argument("Array.get") ->
>           Lazy.force usage;;

>         [knotwell@knotwell stock]$ ./a.out
>         Usage: ./a.out file

The doc says Invalid_argument should be called, but it doesn't say
anything about the string in the exception.  On my alpha with the
current development version of O'Caml, this is "Array.get" for the
byte-code interpreter (which gives the expected result), but
"out-of-bound array or string access" with the native-code compiler.

The bug with uncaught exceptions seems to be architecture-specific, so
you should tell us what kind of machine and system you have.


>As an aside, I don't particular like my use of lazy and force.  Is
>there a cleaner way do the same thing?

Certainly.  Just write:

  let usage () = Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1;;
  let filename =
          try
             Sys.argv.(1)
          with Invalid_argument(_) ->
             usage ();;

Better yet, use the Arg module to parse your command line...

-- Damien