解释范围宏的扩展(Explain expansion of the scope macro)

Phoenix.Router手册页说明 :

scope(path, options, list)

使用给定路径定义范围。

此功能是以下的快捷方式:

scope path: path do ... end

此宏调用中的一个参数是options 。 我认为列表是以do开头并以end 。 有人可以解释options扩展的位置吗?

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?

最满意答案

是的,这里的list是一个包含[do: block]元素列表,其中block是do ... end的代码。 这里的文档不是很清楚,但您可以阅读源代码以更好地理解这一点。

简而言之,如果options是列表,则将path插入到options中,因此:

scope(path, options, list)

相当于

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

如果options是一个列表,等同于

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

如果options是一个atom(被视为作用域的别名)。

参数在文档中命名为list的原因是函数顶部没有函数头声明,这使得ex_doc推断出一个名称,并且它选择使用list因为函数接受列表[do: block] as最后一个论点。 你可以在这里阅读更多相关信息。

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.

更多推荐