scala - 为什么使用 SBT 编译时会出现此错误?
解决方案
理解的关键是给出一系列模式,然后第一个匹配的模式获胜,其余的被忽略
匹配表达式是通过按写入顺序尝试每个模式来评估的。选择第一个匹配的模式,然后选择并执行箭头后面的部分。
例如,考虑下面匹配任何值的变量模式 pattern2
(42: Any) match {
case pattern1: String => "first pattern1 was tried but did not match"
case pattern2 => "then pattern2 was tried and it did indeed match"
case pattern3: Int => "so pattern3 was not tried because pattern2 already won"
}
thenpattern3
将永远不会被尝试,并且整个匹配表达式的计算结果为
then pattern2 was tried and it did indeed match
而编译器发出警告
Warning:(19, 28) unreachable code due to variable pattern 'pattern2' on line 22
case pattern3: Int => "so pattern3 was not tried because pattern2 already won"
这不是错误,而只是指定编译器的工作方式,以避免永远不会执行的错误或无法访问的代码。另一种思考方式是将模式匹配想象成一个if-else
s链
if (cond1) {
"first cond1 was tried but did not match"
} else if (cond2) {
"then cond2 was tried and it did indeed match"
} else if (cond3) {
"so cond3 was not tried because cond2 already won"
} else {
// crash
}
推荐阅读
- google-sheets - 如何从字符串中提取文本?
- javascript - Javascript将多维数组转换为索引
- excel - Excel VBA - 在一个单元格中合并具有重复值的行并合并其他单元格中的值
- python - django.db.utils.IntegrityError:唯一约束失败:new__bank_profile.fname_id
- pandas - 如何将熊猫数据框写入 .arrow 文件
- java - java.lang.NullPointerException:无法调用“org.json.JSONObject.getJSONObject(String)”,因为“a1.JsonConfig.configProperties”为空
- php - 将 url 提供为 dir/file/file 时出现 500 内部错误
- javascript - 我如何检查 onclick 道具比 redux 状态更快
- javascript - 当我 res.redirect(`/relative/url`) 时,收到的响应是 PATCH 404 Not found。如何解决这个问题?
- tensorflow - 如何在一个纪元结束时注销 Keras 自定义损失组件?