我第一次注意到了尾隨感嘆號標記/感嘆號(!)經歷鳳凰教程(在Incoming Events部分),而什麼是Elixir Bang功能?
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
是什麼後感歎號的意思是一個函數?它有什麼作用嗎?我一直在四處搜尋,並試圖尋找,但我不知道我使用正確的條款。到目前爲止,僅僅作爲約定的函數似乎會在錯誤失敗時引發錯誤,但它總是意味着錯誤。
唯一提到我看到它出現在「編程花好月圓」由戴夫·托馬斯:
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...
這些都不解釋了,如果這是一個約定其他藥劑師或煉金術士或任何使用。請幫忙。
感謝您花時間從鳳凰來源提供一個很好的例子。它完美地回答了我的問題。我被困在你的答案和我選擇的答案之間,因爲兩者都很好地解釋了我的問題。我選擇不選這個,因爲它專注於鳳凰使用'!',不幸的是,在我的問題中沒有表達出來,我對Elixir的慣例比Phoenix更感興趣。 – Marc
我要吃我自己的單詞 - 並接受你的答案,因爲它指出了@MoxleyStratton提到的一個重要細節 - 「爆炸慣例適用於有兩個版本的函數 - 一個引發異常(爆炸版本),而不是。「 – Marc