cevado

cevado

Any reason for the different semantics with records in Elixir?

So I was experimenting with records in elixir and I found it very odd the different behaviour between using records in erlang.

defmodule R do
  require Record
  Record.defrecord :some, [:a, :b, :c]
end

R.some({:some, 1}, :a)

This would return 1 instead of returning a error of bad record.
while in erlang:

1> rd(some, {a, b, c}).
some
2> B = {some, 1}.
{some,1}
3> B#some.a.
** exception error: {badrecord,{some,1}}

Most Liked

rkallos

rkallos

Unlike in your Elixir example, in your Erlang example, you aren’t actually creating a #some record.

This should work instead:

1> rd(some, {a, b, c}).
some
2> B = #some{a = 1}.
#some{a = 1,b = undefined,c = undefined}
3> B#some.a.
1

The reason the Elixir example works is definitely interesting. It winds up calling :erlang.element(2, {:some, 1}), which only works for the :a field. If you try to get :b or :c, it should raise an exception about the index being out of range.

I suspect the core misunderstanding might be that {:some, 1} is not a valid ‘some’ record. The tuple representation of #some{a=1} is {some, 1, undefined, undefined}.

rkallos

rkallos

both examples do the same.

The intent of the examples is the same, but <record_name>/2 uses :erlang.element/2 to fetch a field. The Elixir example works on the {:some, 1} input, while the Erlang one doesn’t, because it first checks that the input is a ‘tagged tuple’ with the appropriate arity (in this case; 4).

this is exactly what i’m reporting… the elixir version doesn’t identify it as a bad record at all.

Right!

I guess we could conclude that the reason why the semantics are different in Elixir is because the private function Record.get/4 uses the :erlang.element/2 BIF. This avoids checking that the input is a tuple of the correct arity, and that the ‘tag’ element of the tuple matches the record tag. The result of this is that <record_name>/2 will work for tuples that aren’t necessarily records.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement