Search code examples
erlangrecordsdouble-quotes

Erlang 'record' explanation


I was exploring the source code here:

https://github.com/s1n4/leptus/blob/master/include/leptus_logger.hrl

And noticed a record defined like this:

-record(log_data,
    {
      request_time = erlang:localtime() :: calendar:datetime(),
      response_time :: undefined | calendar:datetime(),
      request_line = "" :: string(),
      ip :: inet:ip_address(),
      version = 'HTTP/1.1' :: atom(),
      method = <<"GET">> :: binary(),
      uri = <<"/">> :: binary(),
      headers = [] :: [{binary(), iodata()}],
      status = 200 :: non_neg_integer(),
      content_length = 0 :: non_neg_integer()
    }).

I only know of the 'double colon' used in list comprehensions and types. Have never found anything about records. Searching also didn't help. I'm interpreting it as:

'request_time' is 'erlang:local time()' of type 'calendar:date time()'
response_time is of type undefined or calendar:datetime
and so on ...

Is this correct ?


Solution

  • Yes, you are correct. You can include type information in record definitions. This is one of the coolest aspect of records, actually, and one I see rarely used.

    The part of the docs you are looking for is a bit difficult to find, but it is documented:

    http://www.erlang.org/doc/reference_manual/typespec.html#typeinrecords