MartinElvar
MartinElvar

Reputation: 5804

Is it possible to instantiate a new struct, from a variable, containing a reference to that struct?

Consider the following example.

iex(2)> defmodule User do
...(2)>   defstruct name: "tester"
...(2)> end
{:module, User,
 <<70, 79, 82, 49, 0, 0, 8, 32, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 232,
   0, 0, 0, 22, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 9, 102, ...>>, %User{name: "oste"}}
iex(4)> test = User
User
iex(5)> %test{}
** (CompileError) iex:5: expected struct name to be a compile time atom or alias, got: test

iex(5)> %User{}
%User{name: "tester"}

I'm getting structs passed from configuration, and i want to create new structs based of the specified config.

Upvotes: 3

Views: 707

Answers (1)

NoDisplayName
NoDisplayName

Reputation: 15746

I believe this may work:

test = User
struct(test)

Full info can be found here

Upvotes: 5

Related Questions