[erlang-questions] is_integer should always be used?

David Mercer <>
Thu Jul 3 17:01:41 CEST 2008


On Wednesday, July 02, 2008 at 19:51, Richard A. O'Keefe wrote:
> On 2 Jul 2008, at 8:38 pm, not norwegian swede wrote:
> > when programming erlang, should i always use is_integer if a float
> > will make the function crash?
> 
> Think about WHY you want to use it.
> Do you want to make the function crash?  But you've said it will anyway.
> To make the crash report more helpful?  Could be useful.
> To improve the results from the Dialyzer?  Are you using it?  What
> does it say?
> To express your intentions?  Best of all.

I tend to use the is_integer guard on exported functions that expect an
integer because:

1. Have been told Dialyzer will do better with it.

2. It does express my intention better.

3. There might be compiler optimizations that can be done if the compiler
knows the argument is an integer.  Similar to what you see in Common Lisp.

Also, however, I do not use the guard on nonexported functions, because I
don't need the check being done more than once.  This is kind of in conflict
with #3, but my reasoning is that there is overhead to the check, and any
sufficiently smart compiler can figure out that only integers come in as the
argument based on a Dialyzer-like check, so there should be no need to incur
the overhead again.

My guess is that there are not, as yet, any compiler optimizations done on
integers, but my hope is that there will be in the future.  In the meantime,
I've still got reasons 1 and 2.

Is my reasoning sound, or just fantasy?

Cheers,

David




More information about the erlang-questions mailing list