vrod

vrod

Adding more to __using__?

Is there a way to put more functions into __using__ block? It gets very long and hard to read. I cannot figure out how to split up the quote do into multiple functions. Is there a trick?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yea I agree with @eksperimental here, this use of macros generally causes more trouble than it’s worth.

If you find yourself with functions that take too many arguments, consider creating a struct and then passing on that struct. For example:

defmodule SomeModule do
  defstruct [shared1: "a", shared2: "b", foo: nil, bar: nil]
end

Then you construct a %SomeModule{} struct and it will take on the relevant defaults, and also provide a place to store other values you want to set. Then you can pass this to your various functions, and add extra arguments as appropriate.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@vrod I’ll start by saying: If this is a problem you’re running into, you are 100% putting too much inside of a __using__ block. Code inside of a using block should generally look like this:

defmodule SomeThing do
  defmacro __using__(opts) do
    quote do
      def foo(arg) do
        SomeThing.foo(__MODULE__, arg)
      end
    end
  end

  def foo(module, arg) do
    # complex multi-line implementation here
  end
end

NOT

defmodule SomeThing do
  defmacro __using__(opts) do
    quote do
      def foo(arg) do
        # complex multi-line implementation here
      end
    end
  end
end

Doing it the first way will improve compile times, debugging ability, and avoid the problem you are running into.

BUT, to actually answer your question, you can do this;

defmacro __using__(opts) do
  [
    first_part_stuff(opts),
    second_part_stuff(opts),
  ]
end

defp first_part_stuff(opts) do
  quote do
    # things here
  end
end

defp second_part_stuff(opts) do
  quote do
    # things here
  end
end

Basically, return a list of quote do blocks.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You can @doc false those functions if you want to make it clear that they aren’t supposed to be used.

kip

kip

ex_cldr Core Team

If the only purpose of the __using__ macro is to define functions that have no dependency on macro arguments then I think just import MyModule is far clearer in intent and easier to maintain. I can’t tell if your use case fits this description though.

Otherwise, as @benwilson512 says, the quoted code should do as little as possible and delegate to normal functions early for the same reasons as clear intent and maintainability.

eksperimental

eksperimental

I would recommend against that. Be explicit and avoid magic.
Later you will loose understanding of what’ s going on with your code.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
Tee
can someone please explain to me how Enum.reduce works with maps
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement