Re: small code problem

From: Damien Doligez (
Date: Thu Jul 08 1999 - 20:23:47 MET DST

Date: Thu, 8 Jul 1999 20:23:47 +0200
From: Damien Doligez <>
Message-Id: <>
Subject: Re: small code problem

>From: "<Brad Knotwell" <>

>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 =
          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