[erlang-questions] Runtime checking for types

Ken Robinson kenrobinsonster@REDACTED
Tue Apr 19 16:04:21 CEST 2011


For built in types like integer, list, atom, etc I can do a guard test
like when is_list(MyValue).  For types that I create it would be good
to have a guard test like is_type(mytype, MyValue).

It looks like I can't test for my created types at runtime. I'll use a
hrl file containing my record or the pattern matching.

Is it the case the types I create are only for the benefit of dialyzer?

Ken.




On Mon, Apr 18, 2011 at 8:51 AM, Robert Virding
<robert.virding@REDACTED> wrote:
> 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.
>
> Robert
>
> ----- "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
>



-- 
regards,
Ken Robinson
Mob +61438681120
Home +61738523767



More information about the erlang-questions mailing list