Search code examples
typeserlangtypecheckingcustom-type

Check if variable is of a custom type in Erlang?


Is there a way to check if a variable is of a custom type in Erlang?

Suppose I have some records and types defined in an .hrl file:

-record(custom_record, {
    attr1 :: list(),
    attr2 :: binary(),
    attr3 :: tuple()
}).

-record(another_record, {
    attr1 :: list(),
    attr2 :: binary(),
}).

-type custom_record() :: #custom_record{}.

-type another_record() :: #another_record{}.

-type custom_records() :: custom_record() | another_record().

Is there an easy way to check if a record is a custom_record in my Erlang code? Something like this would be nice:

is_custom_type(CustomRecord, custom_records). %=> true

I looked through the docs and didn't see any build-in functions that did this.


Solution

  • There are no custom types in Erlang. Records are just syntactic sugar for tuples tagged with the atom and of the same length. Typespecs are used solely by dialyzer and nothing else.