In a newly generate phoneix project there is a file hello_web.ex file which has the following code
def router do
quote do
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
end
end
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
What I dont understand here is the use of quote inside router function.
quote will return AST of the contents inside the do end block so how is that helping?why not remove quote and use it like this?
def router do
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
end
In MyAppWeb.Router, there’s use MyAppWeb, :router, which calls the macro MyAppWeb.__using__(:router), which in turn returns the return value of MyAppWeb.router/0. As macros (__using__) do receive AST and need to return AST it further means router/0 does need to return AST. MyAppWeb.router/0 is not meant to ever be called from a place not within a macro. Maybe they should even be private functions.