vrod

vrod

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?

Showing Posts 1 to 8

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.

vrod

vrod OP

I see, thank you. In your example, you end up having more public functions available, yes? I think sometimes this might make a problem?

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.

eksperimental

eksperimental

I don’t know if I quiet understand your question, but you can put as many functions as you please inside the quote do block in using.

For instance:

defmodule Using do
  defmacro __using__(_opts) do
    quote do
      def a(), do: :a

      def b(), do: :b
    end
  end
end

defmodule Foo do
  use Using
end

And then

 $iex -S mix
iex(1)> Foo.a
:a
iex(2)> Foo.b
:b
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.

vrod

vrod OP

I have arguments passing in use, so import does not work (maybe there is a trick?). My purpose is to simplify many functions: when I use with arguments, I can make many functions in a module that are much more simpler. For instance instead of something1(shared1, shared2, x, y), something2(shared1, shared2, x, y), …
I can do use Thing, shared1: "a", shared2: "b" and then have much simpler functions like something1(x, y) because the macro put the shared values where they are needed.

The quote gets long with @doc especially, so even if there is no complex logic in the functions and they are simple calls to some other functions like @benwilson512 says, it is helpful to organize things in smaller pieces sometimes. I hope that makes sense

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.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews