[erlang-questions] Trouble with Erlang's lenient comparisons
Björn Gustavsson
bgustavsson@REDACTED
Thu Apr 14 09:27:23 CEST 2011
On Wed, Apr 13, 2011 at 4:36 PM, 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'
Did you read the note in the documentation for is_record/2?
As long as the RecordTag argument is a literal atom, the
compiler will essentially rewrite it to a call to is_record/3,
which will check the size too.
Therefore, we still recommend that you use is_record/2,
if you are going to use is_record() at all.
Historically, only is_record/2 existed and it was not a
BIF, but specially treated by the compiler. We added the
BIF versions so that it would be possible to use apply on
them, and for consistency with match specs. In most
circumstances, the BIFs will not be called as the compiler
tries to convert calls to is_record/{2,3} to pattern matching
and inline most of the remaining calls.
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list