【发布时间】:2016-01-24 06:52:28
【问题描述】:
在阅读 Phoenix 教程(在 Incoming Events 部分)时,我首先注意到一个带有结尾感叹号/bang(!) 的函数
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
后面的感叹号是什么意思?它有什么作用吗?我一直在四处寻找并尝试寻找,但我不确定我使用的术语是否正确。到目前为止,似乎仅作为约定的函数在失败时会引发错误,但它总是意味着这样。
我看到的唯一提及它出现在 Dave Thomas 的“Programming Elixir”中:
Identifiers in Elixir are combinations of upper and lower case ASCII
characters, digits, and underscores. Function names may end with a
question mark or an exclamation point.
在the documentation 中也提到:
Notice that when the file does not exist, the version with ! raises an
error. The version without ! is preferred when you want to handle
different outcomes using pattern matching...
这些都没有解释这是否是其他长生不老药师或炼金术士或其他任何人使用的惯例。请帮忙。
【问题讨论】: