Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: how to coerce expression type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: how to coerce expression type?
> 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 = 
 try
  let _ = Hashtbl.find table key in
  true
 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 =
     try
       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
     then
      ``code for found case''
     else
      ``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, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/