Issues writing client libraries

Hi all, I've been scratching my head about an issue I've been unable to figure out for a few weeks now.

 

The central gist is this: I'm trying to define a module which contains re-usable functionality (think wrapping queries to DynamoDB or processing messages from SQS) and I'm trying to then pull that functionality into another module which would define, say, its queue_name or table_name

 

defmodule A do
    use B
    def queue_name, do: "test"
end

 defmodule B do
     @callback queue_name
     defmacro __using__(_opts) do
         @behaviour B
     end

     def do_something() do
         # calls queue_name to do something
     end
 end

 

Is there any way to do what I'm trying to achieve? If I try to call queue_name in do_something, I predictably will get a compilation error. Obviously, I could wrap everything I want to do inside B in a big ol quote block, but that seems also wrong

Posted Jan 3, 2018, 12:19 AM UTC

49
vlatheimpaler•

You could try doing something like this:

defmodule B do
  defmacro __using__(opts) do
    quote do
      def queue_name, do: unquote(opts[:queue_name])
    end
  end
end

defmodule A do
  use B, queue_name: "test"
end
2
andyleclairOP•

True, I did think of that, but then I have to put all my code in a quote block.

I think the real solution is to put this sort of configuration info in the proper config script (ie a map of module name to queue name, table name, etc) and then reference it via Application.get_env

1
andyleclairOP•

I think the real problem I'm having is that what I'm trying to do is really more of a Ruby-ism that just doesn't work in Elixir.

 

I want to basically partially define a module B and ensure that the module that includes my module (A) fills in the missing bits, but that I can still reference. Like, if I could define that my module has a callback but then make reference to that callback in some functions I'm providing to the including module.

2
mbuhot•

You can definitely do mixin style modules in elixir. As you mentioned in a previous comment, it requires putting all the mixed-in code in the quote block.

defoverridable and super even allow optional overrides and calling back into the "base" module.

1
andyleclairOP•

Right, I get that. It just doesn't feel right defining a ton of functions inside of a quote block.

It makes me think that there's a better solution that I just don't know yet.

1
mbuhot•

Yeah, you definitely want to minimise the amount of code in the quote block.

Maybe take inspiration from Ecto.Repo? It defines little stubs in your module that then call back into the Ecto library where the real implementations are.

2
andyleclairOP•

Not a bad recommendation, I'll check out the source. Thanks!

2
e456123789•

B needs to receive the module that implements its callbacks as argument. So you would do:

def do_something(module) do
  module.queue_name
end

However, if all you need in do_something is data, then passing a map or keyword with the info may be a better way to go.

1
andyleclairOP•

Agreed.

1