<br><br><div class="gmail_quote">On Sat, Jan 26, 2013 at 9:32 AM, Tyron Zerafa <span dir="ltr"><<a href="mailto:tyron.zerafa@gmail.com" target="_blank">tyron.zerafa@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>Does there exist any particular function which would give me all variable bindings (including their values) at that point. For instance, something similar to get_all_bindings (example below) should result in [{X=3},{Y=4}] when called with X=2.</div>
<div><br></div><div>my_fun(X) -></div><div> Y = X+1,</div><div> get_all_bindings(). </div></div></blockquote><div><br></div><div>I don't think anything like that exists, but here's a parse transform that might work for you:</div>
<div><br></div><div><a href="https://gist.github.com/4643721">https://gist.github.com/4643721</a></div><div><br></div><div>The parse transform looks for a call to get_all_bindings/1 in each function body and replaces its argument with a list of variable names and their bindings used in the function up to that point. The argument to get_all_bindings can be anything because the parse transform replaces it. Example:</div>
<div><br></div><div>-module(foo).</div><div>-export([my_fun/1]).</div><div>-compile([{parse_transform,get_all_bindings}]).</div><div><br></div><div>-record(rec, {f1, f2, f3}).</div><div><br></div><div>my_fun(X) -></div>
<div> Y = X+1,</div><div> Z = [a,b,c,d,e,{X,Y}],</div><div> #rec{f1=F1, f2=F2, f3=F3} = #rec{f1=42, f2=[X,Y], f3=Z},</div><div> get_all_bindings([]),</div><div> Q = 32,</div><div> Q.</div><div><br></div>
<div>get_all_bindings(Bindings) -></div><div> io:format("~p~n", [Bindings]).</div><div><br></div><div>The code produced for my_fun/1 by the parse transform looks like this:</div><div><br></div><div><div>my_fun(X) -></div>
<div> Y = X + 1,</div><div> Z = [a, b, c, d, e, {X, Y}],</div><div> #rec{f1 = F1, f2 = F2, f3 = F3} = #rec{f1 = 42,</div><div> f2 = [X, Y], f3 = Z},</div><div> get_all_bindings([{'Z', Z}, {'Y', Y}, {'X', X},</div>
<div> {'F3', F3}, {'F2', F2}, {'F1', F1}]),</div><div> Q = 32,</div><div> Q.</div></div><div><br></div><div>Note that Q isn't included in the list because it occurs after the call to get_all_bindings/1.</div>
<div><br></div><div>--steve</div><div><br></div><div> </div></div>