Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 603
antipatreal Avatar asked Dec 25 '22 03:12

antipatreal


1 Answers

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

like image 93
zxq9 Avatar answered Jan 05 '23 09:01

zxq9