[erlang-questions] Inheriting functions that use record atoms

Kenny Stone kennethstone@REDACTED
Sat Jul 9 18:55:59 CEST 2011

Records don't play nice with other languages.  I have a similar situation
that I haven't tackled yet with my mnesia store, but I solved it with my ets
store by doing something like:

{objtype, objid, ObjPropList, JSON}

You wouldn't have to keep a copy of the JSON around, but I do because ets is
my last value cache for a web server so I might as well save the
serialization step.  I imagine in mnesia it could be similar, or you could
do something more generic

-record(mdoc, {objid, objtype, obj=[], json}).  % or something

Your record doesn't represent your object at all, it's just something that
mnesia can deal with.  The real object is just a proplist, which is how JSON
usually deserializes into Erlang.


On Sat, Jul 9, 2011 at 8:28 AM, Tristan Sloughter <
tristan.sloughter@REDACTED> wrote:

> I will have a number of modules that define a single record and functions
> for using that record as a model stored in a database for a webapp.
> There are these nice macros for converting a record to json and json to a
> record that will basically always look like:
> to_json(Record) ->
>     ?record_to_json(?MODULE, Record).
> to_record(JSON) ->
>     ?json_to_record(?MODULE, JSON).
> But at times they'll need to be overridden for a specific module/record.
> My problem is I'd like to not have to copy/paste these into every module
> (and they'll be more base functions like these) that is a model. extends
> would be best since that allows overriding but from the base module I can't
> reference the module that is inheriting the base module as a macro, so I
> can't use the json_to_record macro since I can't pass in a macro of the
> record atom name.
> A parse transform won't work either since I don't think I can add a call to
> a macro like I need.
> Does anyone have any suggestions? Or should I just not use records for
> this? Records are nice since I'm using mnesia and found those record_to
> json_to macros!
> Thanks,
> Tristan
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110709/6f33cad2/attachment.htm>

More information about the erlang-questions mailing list