Erlang 'record' explanation

I studied the source code here:

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

And I noticed a record defined as follows:

-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 the "double colon" used in lists and types. Never found anything about records. The search also did not help. I interpret 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 it correct?

+4
source share
1 answer

Yes you are right. You can include type information in record definitions. This is one of the coolest aspects of recordings, in fact, and one that I see rarely used.

Some of the documents you are looking for are a bit hard to find, but they are documented:

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

+9
source

All Articles