record小记

今天阅读rabbitMQ代码,遇到一段代码很疑惑:
try
        log(info, "liufan the #v is ~p~n", [#v1.stats_timer]),
        run({?MODULE, recvloop,
        run({?MODULE, recvloop,
             [Deb, switch_callback(rabbit_event:init_stats_timer(
                                     State, #v1.stats_timer),
                                   handshake, 8)]}),
        log(info, "closing AMQP connection ~p (~s)~n", [self(), Name])
    catch
        Ex -> log(case Ex of
                      connection_closed_abruptly -> warning;
                      _                          -> error
                  end, "closing AMQP connection ~p (~s):~n~p~n",
                  [self(), Name, Ex])
    after


一致没看懂#v1.stats_timer它的意思,以为是这个field的默认值undefined,可是又不对
init_stats_timer(C, P) ->
    {ok, StatsLevel} = application:get_env(rabbit, collect_statistics),
    {ok, Interval}   = application:get_env(rabbit, collect_statistics_interval),   
    setelement(P, C, #state{level = StatsLevel, interval = Interval,               
                            timer = undefined}).


按理说应该是个整数,最后不得看看到底是怎么回事,写了一个测试函数
-module(test).

-export([start/0]).

-record(test, {a,b=1}).

start() ->
        io:format("~p~n", [#test.b]).


生成的abstract code竟然是:

{ok,{test,
        [{abstract_code,
             {raw_abstract_v1,
                 [{attribute,1,file,{"test.erl",1}},
                  {attribute,1,module,test},
                  {attribute,3,export,[{start,0}]},
                  {attribute,5,record,
                      {test,
                          [{record_field,5,{atom,5,a}},
                           {record_field,5,{atom,5,b},{integer,5,1}}]}},
                  {function,7,start,0,
                      [{clause,7,[],[],
                           [{call,8,
                                {remote,8,{atom,8,io},{atom,8,format}},
                                [{string,8,"~p~n"},
                                 {cons,8,
                                     {record_index,8,test,{atom,8,b}},
                                     {nil,8}}]}]}]},
                  {eof,9}]}}]}}


原来是索引值。

对比一下
-module(test1).

-export([start/0]).

-record(test, {a,b=1}).

start() ->
        Tmp = #test{},
        io:format("~p~n", [Tmp#test.b]).


生成的是
{ok,
 {test1,
  [{abstract_code,
    {raw_abstract_v1,
     [{attribute,1,file,{"test1.erl",1}},
      {attribute,1,module,test1},
      {attribute,3,export,[{start,0}]},
      {attribute,5,record,
       {test,
        [{record_field,5,{atom,5,a}},
         {record_field,5,{atom,5,b},{integer,5,1}}]}},
      {function,7,start,0,
       [{clause,7,[],[],
         [{match,8,{var,8,'Tmp'},{record,8,test,[]}},
          {call,9,
           {remote,9,{atom,9,io},{atom,9,format}},
           [{string,9,"~p~n"},
            {cons,9,
             {record_field,9,{var,9,'Tmp'},test,{atom,9,b}},
             {nil,9}}]}]}]},
      {eof,10}]}}]}}


看来还是的找到根源

你可能感兴趣的:(erlang,record)