Re: how to coerce expression type?

From: Pierre Weis (
Date: Wed Dec 23 1998 - 16:01:57 MET

From: Pierre Weis <>
Message-Id: <>
Subject: Re: how to coerce expression type?
To: (Dwight VandenBerghe)
Date: Wed, 23 Dec 1998 16:01:57 +0100 (MET)
In-Reply-To: <> from "Dwight VandenBerghe" at Dec 21, 98 09:17:06 am

> I am converting older ocaml code to version 2.01, and
> find that many expressions that passed through the previous
> compilers fine now exhibit warning errors due to not being
> of unit type. This happens when I evaluate an expression
> for its side effects (the horror, the horror). Here's an
> example:
> let is_in_table table key =
> try
> Hashtbl.find table key;
> true
> with Not_found -> false;;
> I don't want the result back from the lookup, I just want
> to know if the key was present. So how do I "cast" the
> find operation to unit, to quiet the warnings? I've tried
> everything I can think of, things like
> (Hashtbl.find table key) : ()
> but have not yet hit upon the magic incantation.
> Thank you.
> Dwight

There are (at least) three ways to fix the warning:

1) Disable this warning when compiling (ocamlc -ws) as mentioned in
ocamlc -help:
  -w <flags> Enable or disable warnings according to <flags>:
     A/a enable/disable all warnings
     F/f enable/disable partially applied function
     M/m enable/disable overriden methods
     P/p enable/disable partial match
     S/s enable/disable non-unit statement
     U/u enable/disable unused match case
     V/v enable/disable hidden instance variables
     X/x enable/disable all other warnings
     default setting is A (all warnings enabled)

2) Explicitely mention in the source code that you want to throw away
the result of your function, by binding it to a dummy _ identifier:

let is_in_table table key =
  let _ = Hashtbl.find table key in
 with Not_found -> false;;

3) Write a specialized function that returns a meaningful value.

   For instance, suppose we get a warning in a program like this one

   let f table key =
       Hashtbl.find table key;
       (* Key is in table *)
       ``code for found case''
      with Not_found ->
           (* Key is not in table *)
           ``code for not found case''

   We can get rid of the warning (and may be get a clearer code),
   if we write a if then else with a proper predicate as in:
   let f table key =
     if key_is_found table key
      ``code for found case''
      ``code for not found case''

Evidently the key_is_found predicate may be implemented using a
sequence enclosed in a try with construct, so that we need the trick
in 2) to get rid of the sequence warning. But in some cases the
predicate can be implemented more directly (or even more efficiently)
without any sequence or try with.

In your case, the predicate is precisely the is_in_table predicate, so
you should use item 2) or may be 1) if you want a quick fix.

Best regards,
Pierre Weis

INRIA, Projet Cristal,,

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