[erlang-questions] is_integer should always be used?
Kostis Sagonas
kostis@REDACTED
Thu Jul 3 17:24:32 CEST 2008
David Mercer wrote:
> 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.
I want to second the arguments given by David. His programming attitude
towards the use of guards is worth understanding and following.
> 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.
Well, in the native code compiler, there are some optimizations for
integers, especially integers which can be proven to be within a bounded
range.
Kostis
More information about the erlang-questions
mailing list