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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New

We're in Beta

About us Mission Statement