[erlang-questions] Checking Types at Runtime

Joseph Norton norton@REDACTED
Wed Jul 13 16:26:04 CEST 2011


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:

https://github.com/norton/ubf-eep8/blob/master/test/eunit/ubf_eep8_samples.erl

The unit test is here:

https://github.com/norton/ubf-eep8/blob/master/test/eunit/ubf_eep8_samples_tests.erl

The parse transform code is here:

https://github.com/norton/ubf-eep8/blob/master/src/eep8_contract_parser.erl

and finally instructions to download and to build are here:

https://github.com/norton/ubf-eep8

Hope this helps.

- Joe N.


On Jul 13, 2011, at 11:13 PM, Tristan Sloughter wrote:

> 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
> 
> On Wed, Jul 13, 2011 at 4:42 AM, Torben Hoffmann <torben.lehoff@REDACTED> wrote:
> 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
> 
> On Wed, Jul 13, 2011 at 01:26, Tristan Sloughter <tristan.sloughter@REDACTED> wrote:
> 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
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 
> 
> 
> 
> -- 
> http://www.linkedin.com/in/torbenhoffmann
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Joseph Norton
norton@REDACTED



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110713/3b53db9d/attachment.htm>


More information about the erlang-questions mailing list