Error in macro that defines a module with a macro inside

Eh I think defmodule is fine here.

However the issue is that you cannot use bind_quoted in this way (I learned that well long ago). Basically what bind_quoted will do is turn the code into something like this from the return of your macro:

module_name = MySnippet

body =
  (
    def f(x), do: x
    def g(x), do: x
  )

defmodule(module_name) do
  @doc false 
  defmacro(__using__(_opts)) do
    quote do
      body
    end
  end
end

And this is why you are getting the def is not allowed here message. I’ve always had to do ‘fun’ unquote messing when I had to do similar things. So if you Macro.escape the body in bind_quoted then use unquote(body) inside the function then it might work (you might need to ‘escape’ the inner quote though or something like that, I usually just don’t use bind_quoted at all in this case and just unquote through the layers as needed or build the AST manually without quote…).