What does "#" mean in Erlang syntax?
For example:
Request#radius_packet.attrs
Hash marks can mean two things in Erlang: reference to a record, or reference to a map.
The specific case above is referencing a record with the variable name Request, of the type radius_packet, and accessing field attrs. This syntax mimics accessing a field on a struct or object in other languages (but be careful because it is not the same). It is directly equivalent to referencing that field as part of a variable assignment and then using the variable. The three versions of some_function/1 below are all equivalent in terms of what they pass to do_something/1:
some_function(Request = #radius_packet{attrs = Attrs}) ->
do_something(Attrs),
% Other things where we need Request also...
some_function(#radius_packet{attrs = Attrs}) ->
do_something(Attrs),
% Other things where we don't need Record...
some_function(Request) ->
do_something(Request#radius_packet.attrs),
% etc...
Records are a meta-syntax; they are a preprocessor convenience which is actually translated to tuples before compilation (which is why records are so fast). So given the following definition of #radius_packet{} the following version of some_function is exactly equivalent to the ones above:
-record #radius_packet{serial, headers, attrs}.
some_function({radius_packet, _, _, Attrs}) ->
do_something(Attrs),
% Other things where we don't need Record...
The above version has simply ignored record syntax in favor of writing out the underlying tuple that will be created by the preprocessor.
Erlang docs page on records.
The other place you will see hashes is in maps. Map syntax using hashes looks similar to record syntax, but without any type name between the hash and the opening curley bracket:
AMap#{}
ARecord#record_type{}
Erlang docs page on map expressions.
Request is the variable the record is bound to.
# denotes the variable is a record.
radius_packet is the name of the record.
attrs is the field you're accessing from the record.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With