Re: small code problem

From: Damien Doligez (Damien.Doligez@inria.fr)
Date: Thu Jul 08 1999 - 20:23:47 MET DST


Date: Thu, 8 Jul 1999 20:23:47 +0200
From: Damien Doligez <Damien.Doligez@inria.fr>
Message-Id: <199907081823.UAA20553@tobago.inria.fr>
To: caml-list@inria.fr
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



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:23 MET