[erlang-questions] Reassigning variables
Richard O'Keefe
ok@REDACTED
Mon Mar 23 03:13:21 CET 2009
On 18 Mar 2009, at 7:07 pm, Matthew Dempsky wrote:
> Also, here's a list of a few other functions in OTP that use numbered
> variables:
>
> - zip:put_z_files/6
> - epp:predef_macros/1
predef_macros(File) ->
Ms0 = dict:new(),
Ms1 = dict:store({atom,'FILE'}, {none,[{string,1,File}]}, Ms0),
Ms2 = dict:store({atom,'LINE'}, {none,[{integer,1,1}]}, Ms1),
Ms3 = dict:store({atom,'MODULE'}, undefined, Ms2),
Ms4 = dict:store({atom,'MODULE_STRING'}, undefined, Ms3),
Ms5 = dict:store({atom,'BASE_MODULE'}, undefined, Ms4),
Ms6 = dict:store({atom,'BASE_MODULE_STRING'}, undefined, Ms5),
Machine = list_to_atom(erlang:system_info(machine)),
Ms7 = dict:store({atom,'MACHINE'}, {none,[{atom,1,Machine}]}, Ms6),
dict:store({atom,Machine}, {none,[{atom,1,true}]}, Ms7).
This really doesn't need them. It would be better as
predef_macros(File) ->
Machine = list_to_atom(erlang:system_info(machine)),
dict:from_list([
{{atom,'FILE'}, {none,[{string,1,File}]}},
{{atom,'LINE'}, {none,[{integer,1,1}]}},
{{atom,'MODULE'}, undefined},
{{atom,'MODULE_STRING'}, undefined},
{{atom,'BASE_MODULE'}, undefined},
{{atom,'BASE_MODULE_STRING'}, undefined},
{{atom,'MACHINE'}, {none,[{atom,1,Machine}]}}.
{{atom,Machine}, {none,[{atom,1,true}]}
]).
There's a tiny performance loss, but since it's done once per file,
it's not worth worrying about.
More information about the erlang-questions
mailing list