Shun_di

Shun_di

Extract Erlang record into Elixir

Hi,

I’m just starting out with Elixir, however I have used Erlang for about half a year so its not to hard to make the jump across.

However, I am having real trouble with using Erlang records in Elixir.

I have an existing hrl file in a dependency that has two records. I want to be able to extract them and then populate them for use elsewhere.

I am using the following syntax to try and extract them:

Record.defrecord :record_name, Record.extract(:record_name, from: "deps/mydependency/include/header_file.hrl")

However, when I try to compile this I get the following:

== Compilation error in file lib/otherdb.ex ==
** (ArgumentError) cannot invoke defmacro/2 inside function/macro
    (elixir) lib/kernel.ex:5187: Kernel.assert_no_function_scope/3
    (elixir) lib/kernel.ex:3952: Kernel.define/4
    (elixir) expanding macro: Kernel.defmacro/2
    lib/otherdb.ex:28: OHTERDB.defname/0
    (elixir) expanding macro: Record.defrecord/2
    lib/otherdb.ex:28: OHTERDB.defname/0

I’ve seen many other examples of people using this syntax, so I must be doing something wrong.
Any help on this would be appreciated.

Marked As Solved

rvirding

rvirding

Creator of Erlang

IIRC (again) make the argument to new a keyword list. It would become something like:

thing1 = foo(name: "bob", age: 30, job: "Elixir Dev")

Don’t forget that Erlang strings and Elixir strings are not the same.

Also Liked

easco

easco

It looks like you are trying to make your defrecord call from inside of a function like:

def  somefunction() do
  Record.defrecord :record_name...
  …
end

The error says the defrecord call must be moved out of the function, presumably to the level of the module.

rvirding

rvirding

Creator of Erlang

IIRC defrecord defines a record, it does not create an instance. Pretty much the same as -record(Name, {...}). in Erlang.

NobbZ

NobbZ

You are right, and Record.extract/2 extracts a keywordlist from a *.hrl, which can be used in Record.defrecord.

But it seems as if he were trying to extract a record from a dependency, so it should be :from_lib instead of :from.

Additionally what @easco said seems to be valid as well:

** (ArgumentError) cannot invoke defmacro/2 inside function/macro

Proof:

iex(1)> defmodule M do              
...(1)>   require Record
...(1)>   def foo() do
...(1)>     Record.defrecord(:foo, [bar: 0])
...(1)>   end
...(1)> end
  iex:1

** (ArgumentError) cannot invoke defmacro/2 inside function/macro
    (elixir) lib/kernel.ex:5150: Kernel.assert_no_function_scope/3
    (elixir) lib/kernel.ex:3906: Kernel.define/4
    (elixir) expanding macro: Kernel.defmacro/2
    iex:5: M.foo/0
    (elixir) expanding macro: Record.defrecord/2
    iex:5: M.foo/0

As you can see, the errormessage has the same shape.

Shun_di

Shun_di

Thanks for responses guys.
Yep, I’m a bit of an idiot.

easco is right, I had put this in a function whereas it should have been at module level. I corrected this and it was fine.

The next thing I am trying to do is populate the record that has been defined.

In Erlang I can do this by doing the following (I need them to go into a list hence the square brackets):

things = [#thing{name = “bob”, age = 30, job = “Elixir Dev”}]

I thought I could do it something like this based on research but I’m probably/most likely wrong (this is without it being a list):

thing2 = thing.new(
name = “bob”,
age = 30,
job = “Elixir Dev”)

NobbZ

NobbZ

Can you please show the line how you are extracting the record?

And how are you specifiying the dependency in your mixfile?

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement