在阅读书中关于错误处理的章节时,我想知道经常提到的"显式案例分析"的对立面是什么。我知道并理解这个代码示例使用了显式的案例分析:
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CliError::Io(ref err) => write!(f, "IO error: {}", err),
CliError::Parse(ref err) => write!(f, "Parse error: {}", err),
}
}
但是什么是隐式案例分析?
在本书中,case analysis是指通过直接分析enum
的每个case来执行计算,例如使用match
或if let
表达式。书中给出的一个极端例子是
fn file_path_ext_explicit(file_path: &str) -> Option<&str> {
match file_name(file_path) { // <-- case analysis
None => None,
Some(name) => match extension(name) { // <-- another case analysis
None => None,
Some(ext) => Some(ext),
}
}
}
显式案例分析意味着显式地使用"案例分析"。
不存在"隐式案例分析"。本书建议将将常用的案例分析模式抽象到可组合的方法或宏中,也许这就是您所想的。
例如,我们可以在Option<T>
上隐藏and_then
方法中的案例分析(match
表达式):
fn and_then<F, T, A>(option: Option<T>, f: F) -> Option<A>
where F: FnOnce(T) -> Option<A> {
match option { // <-- case analysis moved into here
None => None,
Some(value) => f(value),
}
}
file_path_ext_explicit
函数可以简化为
fn file_path_ext(file_path: &str) -> Option<&str> {
// no `match` expressions
file_name(file_path).and_then(extension)
}
,它更清楚地表达了函数的意图,更不容易出现逻辑错误。