raarts
raarts

Reputation: 2961

Explain expansion of the scope macro

The Phoenix.Router manual page states:

scope(path, options, list)

Define a scope with the given path.

This function is a shortcut for:

scope path: path do
  ...
end

One of the parameters in this macro invocation is options. I thought that the list was the part that starts with do and ends with end. Can someone kindly explain where options is expanded?

Upvotes: 0

Views: 62

Answers (1)

Dogbert
Dogbert

Reputation: 222050

Yes, the list here is a one element list that contains [do: block] where block is the code within do ... end. The documentation is not very clear here, but you can read the source to understand this better.

In short, path is inserted into options if options is a list, so:

scope(path, options, list)

is equivalent to

scope([path: path] ++ options, list)

if options is a list, and equivalent to

scope([path: path, alias: options], list)

if options is an atom (which is treated as the scope's alias).

The reason the argument is named list in the docs is that the function doesn't have a function head declaration on top, which makes ex_doc infer a name, and it chooses to use list since the function accepts a list [do: block] as the last argument. You can read more about this here.

Upvotes: 1

Related Questions