[erlang-questions] Runtime checking for types

Robert Virding robert.virding@REDACTED
Mon Apr 18 00:51:50 CEST 2011

I think the thing to remember is that records are not separate data types. At run-time a record is just a tuple which is large enough to contain all the fields (and only the field values) and the first element of the tuple is an atom which is the name of the record. As such records only exist at compile-time and the compiler converts ALL record operations to the corresponding tuple operations.

Your headerrec record just becomes the tuple {headerrec,MessageId}.

So actually Erlang does not really have proper abstract data types.


----- "Ken Robinson" <kenrobinsonster@REDACTED> wrote:

> I know this sounds crazy but I was wondering if there was some
> runtime
> check for types. I've created an abstract data type in an .erl (see
> below). I want to now pattern match
> -opaque u16int() :: 0..(1 bsl 16 - 1).
> -export_type([u16int/0]).
> ...
> -record(headerrec, {message_id :: u16int()}).
> -opaque headerrec() :: #headerrec{}.
> -export_type([headerrec/0]).
> I can do a runtime test like so:
> foo(#headerrec{} = HeaderRec) ->
>     ...
> What I would like to do is test for the new type similar to the
> gaurds
> is_record() and the like. Any ideas? I realize I can put this all in
> a
> .hrl file but this would break the encapsualtion.
> regards,
> Ken Robinson
> Mob +61438681120
> Home +61738523767
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list