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

Mazen Harake mazen.harake@REDACTED
Thu Apr 14 09:09:44 CEST 2011


Actually they end up being essentially the same thing.

Check out Number 6 in this list:

http://mazenharake.wordpress.com/2010/10/31/9-erlang-pitfalls-you-should-know-about/

I believe the #rec{} notation is preferred for clarity and I agree but
in my opinion is_record/2 is just as correct (literally)

/M

On 13 April 2011 16:36, Gordon Guthrie <gordon@REDACTED> wrote:
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list