wwaldner

wwaldner

Defstruct inside __using__`

Is it possible to define a struct inside a using

defmodule Structor do
  defmacro __using__(_) do
    quote do
      defmodule __MODULE__ do
        defstruct [:field]
      end

      def create(field) do
        %__MODULE__{field: field}
      end
    end
  end
end

defmodule MyStruct do
  use Structor

end

Marked As Solved

sodapopcan

sodapopcan

You don’t need the defmodule __MODULE__ do part:

defmodule Structor do
  defmacro __using__(_) do
    quote do
      defstruct [:field]

      def create(field) do
        %__MODULE__{field: field}
      end
    end
  end
end

defmodule MyStruct do
  use Structor
end

iex> MyStruct.create(:foo)
%MyStruct{field: :foo)

Also Liked

wwaldner

wwaldner

Yes, I had this originally but was was not able to get it to work because I had something else wrong. Thanks for pointing out that it should work.

Last Post!

wwaldner

wwaldner

Yes, I had this originally but was was not able to get it to work because I had something else wrong. Thanks for pointing out that it should work.

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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement