簡體   English   中英

在為函數子句定義守衛時,應該有多嚴格?

[英]How strict should one be when defining guards for function clauses?

當我定義我的函數子句時,我應該有多嚴格?

Elixir允許編寫功能頭,范圍從無保護,超過檢查類型驗證范圍內的值

所以例如我不知道在這樣的函數中使用什么樣式:

def measure(args = %{times: times, path: path}) do ...

def measure(args = %{times: times, path: path}) when is_integer(times) and is_binary(path) do ...

def measure(args = %{times: times, path: path}) when is_integer(times) and times > 0 and is_binary(path) ...

在檢查許多條件時當然是一個問題:線路太長,並且為防護器增加一條線對我來說似乎不是一個好主意。 這使得區分實際代碼和函數頭更加困難......

對於我的函數定義的嚴格性 ,你會建議什么?

使用警衛完全可以檢查數據是否有內容原因。 但是,使用Dialyzer( https://github.com/jeremyjh/dialyxir )可能會更好地確保類型安全。

即使完全不使用防護裝置也會在其他地方導致匹配錯誤。 因此,如果您需要確保提供給您的數據對您的算法有效,請盡早保護它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM