[erlang-questions] Checking Types at Runtime

39 views
Skip to first unread message

Tristan Sloughter

unread,
Jul 12, 2011, 7:26:19 PM7/12/11
to erlang-q...@erlang.org
I'm pretty sure this isn't possible but I wanted to be sure before working around the lack of it.

if I have a type and record like:

-type password() :: binary().

-record(user, {username :: binary(),
                    password  :: password()}. 

At run time there is no way to tell a field is suppose to be of some type during runtime, right? 

Basically I'd like to be able to have generic or generated create functions for records that may have to modify arguments (like a password has to be encrypted from the string provided) when creating the record. So the create function would pass each field, its value and its type to a transform function that matches on type and returns the appropriately modified value.

Any ideas on how to do this with type definitions or do I need to use tuples and an atom like {Type, Value} for the value of every field to achieve this?

Thanks,
Tristan

Eric Merritt

unread,
Jul 12, 2011, 8:31:32 PM7/12/11
to Tristan Sloughter, erlang-q...@erlang.org
Thats not possible at the moment. Dialyzer will do a good job at
compile time warning you that something is wrong. I think thats about
your best bet.

Now, as I understand it this type information *is* retained in the
compiled beam file. So you could possibly write something that will
check the types for you, but I don't think it would be very
performant.

I think the right answer, at least with out more details, is to do a
good job declaring specs and using them, while relying on dialyzer to
tell you when there are problems.

> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Eric Merritt

unread,
Jul 12, 2011, 9:03:58 PM7/12/11
to Tristan Sloughter, erlang-q...@erlang.org
Maybe its worth parsing at initialization and storing somewhere then,
better to define the types once in the spec and reuse them then force
the dev to define them multiple times and keep them in sync. Whether
you can do that or not will depend very much on the system you are
writing of course.

On Tue, Jul 12, 2011 at 7:35 PM, Tristan Sloughter
<tristan....@gmail.com> wrote:
> OK, that is what I thought. But declaring specs and relying on dialyzer for
> problems doesn't help when I want to match on a type. But I guess I'll have
> to either bundle the "type" with the value of the record field. Or have some
> module that accepts a record field atom and returns its "type"...
> Tristan

Torben Hoffmann

unread,
Jul 13, 2011, 5:42:01 AM7/13/11
to Tristan Sloughter, erlang-q...@erlang.org
For the basic types you can you a when guard in the function clauses (or case clauses):

my_magic(#user{password=PW}) when is_binary(PW) -> ...

But that is probably not enough for you.

Cheers,
Torben

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions




--
http://www.linkedin.com/in/torbenhoffmann

Tristan Sloughter

unread,
Jul 13, 2011, 10:13:19 AM7/13/11
to Torben Hoffmann, erlang-q...@erlang.org
Yeah, I was hoping for something that would work with user defined typed.

I assume parse transforms when parsing a record have the type information? If so I think I can use that to generate the necessary functionality I'm looking for.

Tristan

Joseph Norton

unread,
Jul 13, 2011, 10:26:04 AM7/13/11
to Tristan Sloughter, erlang-q...@erlang.org

Tristan -

It has been awhile since I dabbled with this code - it is an incomplete prototype to re-use a module's types as part of a UBF contract.  Nevertheless, there is a working unit test that illustrates using a parse transform on a module's types.

The test module is here:


The unit test is here:


The parse transform code is here:


and finally instructions to download and to build are here:


Hope this helps.

- Joe N.
Joseph Norton



Tristan Sloughter

unread,
Jul 13, 2011, 10:27:34 AM7/13/11
to Joseph Norton, erlang-q...@erlang.org
Great, thanks! This should help a lot.
Reply all
Reply to author
Forward
0 new messages