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
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
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
IIRC defrecord defines a record, it does not create an instance. Pretty much the same as -record(Name, {...}). in Erlang.
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









