From: Pierre Weis <Pierre.Weis@inria.fr>
To: dwight@pentasoft.com (Dwight VandenBerghe)
Cc: caml-list@inria.fr
Subject: Re: how to coerce expression type?
Date: Wed, 23 Dec 1998 16:01:57 +0100 (MET) [thread overview]
Message-ID: <199812231501.QAA17904@pauillac.inria.fr> (raw)
In-Reply-To: <3.0.32.19981221091704.00bcd310@seanet.com> 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 =
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/
prev parent reply other threads:[~1998-12-23 15:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-12-21 17:17 Dwight VandenBerghe
1998-12-23 15:01 ` Pierre Weis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199812231501.QAA17904@pauillac.inria.fr \
--to=pierre.weis@inria.fr \
--cc=caml-list@inria.fr \
--cc=dwight@pentasoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox