E4186#
Compiler diagnostic name: guard_not_supported_in_longest_match_strategy.
最长匹配策略的 lexmatch 分支不支持守卫条件。
最长匹配策略会在运行分支主体之前决定哪个正则分支胜出。分支守卫会让这个决定依赖任意表达式,因此在 lexmatch ... with longest 中会被拒绝。
错误示例#
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
"abc" if text.length() > 0 => ()
_ => ()
}
}
第一个分支带有 if 守卫。
修改建议#
将条件移入分支主体,让最长匹配的决定只依赖正则模式。
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
"abc" => if text.length() > 0 { () }
_ => ()
}
}