Authors: : Ulf Wiger (ulf.wiger@ericsson.com).
This parse transform can be used to reduce compile-time dependencies in large systems.
In the old days, before records, Erlang programmers often wrote access functions for tuple data. This was tedious and error-prone. The record syntax made this easier, but since records were implemented fully in the pre-processor, a nasty compile-time dependency was introduced.
This module automates the generation of access functions for records. While this method cannot fully replace the utility of pattern matching, it does allow a fair bit of functionality on records without the need for compile-time dependencies.
Whenever record definitions need to be exported from a module,
inserting a compiler attribute,
export_records([RecName|...])
causes this transform
to lay out access functions for the exported records:
-module(foo) -compile({parse_transform, dia_exprecs}). -record(a, {a, b, c}). -export_records([a]). -export(['#info-'/1, '#info-'/2, '#get-'/2, '#set-'/2, '#new-a'/0, '#new-a'/1, '#get-a'/2, '#set-a'/2, '#info-a'/1]). '#info-'(Rec) -> '#info-'(Rec, fields). '#info-'(a, Info) -> '#info-a'(Info). '#new-a'() -> #a{}. '#new-a'(Vals) -> '#set-a'(Vals, #a{}). '#get-'(Attrs, Rec) when is_record(Rec, a) -> '#get-a'(Attrs, Rec). '#get-a'(Attrs, R) when is_list(Attrs) -> ['#get-a'(A, R) || A <- Attrs]; '#get-a'(a, R) -> R#a.a; '#get-a'(b, R) -> R#a.b; '#get-a'(c, R) -> R#a.c. '#set-'(Vals, Rec) when is_record(Rec, a) -> '#set-a'(Vals, Rec). '#set-a'(Vals, Rec) -> F = fun ([], R, _F1) -> R; ([{a, V} | T], R, F1) -> F1(T, R#a{a = V}, F1); ([{b, V} | T], R, F1) -> F1(T, R#a{b = V}, F1); ([{c, V} | T], R, F1) -> F1(T, R#a{c = V}, F1) end, F(Vals, Rec, F). '#info-a'(fields) -> record_info(fields, a); '#info-a'(size) -> record_info(size, a).
context/2 | |
format_error/1 | |
parse_transform/2 |
context(X1, Context) -> any()
format_error(X1) -> any()
parse_transform(Forms, Options) -> any()
Generated by EDoc, Jan 10 2010, 19:56:29.