在折騰枚舉作為子例程布爾選項的時候, 我發(fā)現(xiàn)默認的錯誤信息不夠酷诺擅。
Constraint type check failed for parameter '@options'
讓錯誤信息變得更具體有點困難。我們來創(chuàng)建幾個 exceptions 來告訴我們當東西出錯時究竟發(fā)生了什么苍碟。
class X::Paramenter::Exclusive is Exception {
has $.type;
method message {
"Parameters of {$.type.perl} are mutual exclusive"
}
}
現(xiàn)在我們能檢查 Find::Type 的選項是否是獨占的從而拋出異常撮执。
&& ( exclusive-argument(@options, Find::Type) or fail X::Paramenter::Exclusive.new(type => Find::Type) )
class X::Parameter::UnrecognisedOption is Exception {
has $.type;
has $.unrecognised;
method message {
"Option { $.unrecognised } not any of { $.type.map({ (.^name ~ '::') xx * Z~ .enums.keys.flat }).flat.join(', ') }"
}
}
因為枚舉對類型而言是容器, 我們能使用集合操作符來檢查并挑選出不匹配的選項(基本上, +options 吞噬的任何東西我們都不知道)。
or fail X::Parameter::UnrecognisedOption.new(type => (Find::Type, Find::Options),
unrecognised => .item ? (|Find::Type::.values, |Find::Options::.values) )
把錯誤信息拼接在一塊兒有點復雜因為我們可以在一個給定的枚舉中得到所有枚舉鍵的一個列表, 除了那些不知道它們的限定名的蜓肆。 我們不得不在枚舉名的前面手動前置一個 ::
谋币。
class X::Parameter::UnrecognisedOption is Exception {
has $.type;
has $.unrecognised;
method message {
"Option { $.unrecognised } not any of { $.type.map: { (.^name ~ '::') xx * Z~ .enums.keys.flat } }"
}
}
這導致了一個更令人驚嘆的錯誤信息:
Option 42 not any of Type::File, Type::Dir, Type::Symlink, Options::Recursive, Options::Keep-going
這看起來都很普通。我們擁有一個參數(shù)化的吞噬參數(shù), 它帶有一個或多個枚舉而那些枚舉可能擁有一個 flag 告訴它們是否用作單選按鈕瑞信。聽起來這個 idiom 會很適合這個模塊。
翻譯的不好, 最好看原文逼友。我也不知道 LTA 是什么意思