[erlang-questions] Trouble with Erlang's lenient comparisons

Gordon Guthrie gordon@REDACTED
Wed Apr 13 16:36:46 CEST 2011


Kostis

I have never read the documentation on is_record/2 before (didn't even
know there was is_record/3)

I *assumed* that the is_record guard checked that it indeed was a
record - it would appear it doesn't.

The documentation also explictly says to use is_record/2

> Note!
> This BIF is documented for completeness. In most cases is_record/2 should be used.

If you shouldn't use it then the documentation should probably say so.

I presume you are saying that using #rec{} in the function call is
correctly expanded by the preprocessor to {rec, _, _, } and is
therefore a better specification of the contract than is_record/2
which just says 'it is a tuple with first element of this atom'

Gordon


On 13 April 2011 11:36, Kostis Sagonas <kostis@REDACTED> wrote:
> Gordon Guthrie wrote:
>>
>> ...
>>
>> Also check for bad input to functions by using guards like:
>>
>> fun(A, B, C) when is_list(A), is_integer(B), is_record(C, recC) ->
>
> The following is a bit pedantic, but please do not offer use of the
> is_record guard (esp. the one with arity two instead of three) as advice to
> newcomers.  I suggest that you try to forget about this guard's existence;
> the only place where this guard is possibly needed is in or/orelse contexts.
>
> The above code is written better using pattern matching, as in:
>
>  fun(A, B, #recC{} = C) when is_list(A), is_integer(B) ->
>
> Kostis
>



-- 
Gordon Guthrie
CEO hypernumbers

http://hypernumbers.com
t: hypernumbers
+44 7776 251669



More information about the erlang-questions mailing list