Does Erlang record name allow period inside the name?

A coworker of mine wanted to use an Erlang record name with a period inside, such as:

Variable#'record.name'.field

(Note that quotes are required to show that the record.name is an atom.)

Is this possible? Or is this prohibited?

I suggested the coworker to avoid using this confusing naming, but I’m curious if this is technically possible.

Regards,
Kenji

2 Likes

FYI: this code compiled with no error and worked OK.

-module(record_period).

-export([test/0]).

-record('period.name',
        {foo = 0,
         bar = 1}).

test() ->
        V1 = #'period.name'{foo = 100, bar = 200},
        test2(V1).

test2(#'period.name'{foo = Vfoo, bar = Vbar}) ->
        io:format("foo = ~p, bar = ~p~n", [Vfoo, Vbar]).

Execution result:

Erlang/OTP 25 [erts-13.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns] [dtrace] [sharing-preserving]

Eshell V13.1  (abort with ^G)
1> l(record_period).
{module,record_period}
2> record_period:test().
foo = 100, bar = 200
ok
1 Like

The only requirement is that the record name is a valid atom, which in this case it is though it has to be quoted. The same applies to the keynames in a record, they have to be valid atoms. So the following is a perfectly legal record:

-record('Stuff.Data',{'Data.Field1', 'Data.Field2'}).

I have seen record and field names like this in Ericsson code.

Btw these are the same rules as for function and module names, they have to be valid atoms.

6 Likes

If I not mistake, Elixir does this a lot, for example, a module Namespace.MyModule will became ‘Namespace.MyModule’ and a function myfun! wil be ‘myfun!’.

2 Likes

Yes, but that is because Elixir has a bit more complex syntax with special case handling of module names and atoms. So atoms can END in ! or ? without any need for quoting and names STARTING with an uppercase are module names which apart from the “normal” atom syntax also can contain .. Note that module names actually expand to an atom prefixed by Elixir. so the Foo.Bar ==> Elixir.Foo.Bar. This makes it much easier to have a “separate” Elixir module namespace as all module names must be atoms.

6 Likes

Robert, I’m very much appreciated for the clarification. I’ll tell the coworker that he’s doing the right thing :slight_smile:

2 Likes