[erlang-questions] Debug support on for guards?

Richard O'Keefe <>
Mon May 3 01:21:26 CEST 2010


On May 2, 2010, at 7:52 PM, Henning Diedrich wrote:

> Hi folks,
>
> is there a way to add detail to a function_clause error messages? Be  
> it guards or patterns that don't match?
>
> On a crash, I would like to clarify what guard failed, or even warn  
> the programmer that this guard exists when it may not be intuitive.

Why?  What is the programmer supposed to do about it?
More to the point, what is the *program* supposed to do about it?

>
> 1
>
> This is written, I believe, as it should:
>
> milli_epoch({Mega, Sec, Micro}) when
>   is_integer(Mega), is_integer(Sec), is_integer(Micro),
>   Mega >= 0, Sec >= 0, Micro >= 0 ->
>    Mega * ?BILLION + Sec * 1000 + trunc(Micro / 1000).

In a strictly typed language like Ada, one would arrange for it
to be _impossible_ to construct a record that didn't pass the test.

Is this something the Dialyzer can help with?

Why not separate the task into two issues:
(A) do the main job
(B) diagnose the problem
As a general rule, doing this means that someone can call the (B)
function for some value long before calling the (A) function.

The (A) function can then be streamlined.
For example, the Mega >= 0, Sec >= 0, Micro >= 0 tests can be
removed.  Not only is the function perfectly safe without them,
but one expects to be able to do arithmetic on timestamps, and
that means that if {M,S,U} is valid, one also *expects* that
{-M,-S,-U} should be valid.  (M,S,U should all be non-negative
or all be non-positive, but that's not what the code above tests.)



More information about the erlang-questions mailing list