"""Offline Scala semantic verification lab for Chapter 11 (if-else)."""
import json
VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"
def case_if_else_branch_paths():
def single_if(x):
return "x < 20" if x < 20 else ""
def two_way(x):
return "x 小于 20" if x < 20 else "x 大于等于 20"
expected = {
"single_if_10": "x < 20",
"single_if_25": "",
"two_way_30": "x 大于等于 20",
"two_way_15": "x 小于 20",
}
actual = {
"single_if_10": single_if(10),
"single_if_25": single_if(25),
"two_way_30": two_way(30),
"two_way_15": two_way(15),
}
return {
"name": "if_else_branch_paths",
"summary": "验证单分支 if 与双分支 if...else 在条件真假时的执行路径",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_else_if_and_nested_conditions():
def multi_branch(x):
if x == 10:
return "X 的值为 10"
elif x == 20:
return "X 的值为 20"
elif x == 30:
return "X 的值为 30"
else:
return "无法判断 X 的值"
def nested(x, y):
if x == 30:
if y == 10:
return "X = 30 , Y = 10"
return "not matched"
expected = {
"multi_30": "X 的值为 30",
"multi_99": "无法判断 X 的值",
"nested_30_10": "X = 30 , Y = 10",
"nested_30_20": "not matched",
}
actual = {
"multi_30": multi_branch(30),
"multi_99": multi_branch(99),
"nested_30_10": nested(30, 10),
"nested_30_20": nested(30, 20),
}
return {
"name": "if_else_if_and_nested_conditions",
"summary": "验证多分支 else if 链与嵌套 if 语句的顺序求值逻辑",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_expression_return_value():
# Scala if/else is an expression replacing Java ternary operator
def max_val(a, b):
return a if a >= b else b
status = "pass" if 85 >= 60 else "fail"
expected = {"max_val": 42, "status": "pass", "inferred_same_type": "Int"}
actual = {"max_val": max_val(17, 42), "status": status, "inferred_same_type": "Int"}
return {
"name": "if_expression_return_value",
"summary": "验证 Scala if/else 作为表达式直接返回值并替代三元运算符的语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_without_else_unit_type():
def infer_if_type(then_type, else_type=None):
et = else_type if else_type is not None else "Unit"
if then_type == et:
return then_type
if then_type in ("Int", "Double", "Unit") and et in ("Int", "Double", "Unit"):
return "AnyVal"
return "Any"
expected = {
"int_and_int": "Int",
"int_without_else": "AnyVal",
"string_without_else": "Any",
"unit_without_else": "Unit",
}
actual = {
"int_and_int": infer_if_type("Int", "Int"),
"int_without_else": infer_if_type("Int"),
"string_without_else": infer_if_type("String"),
"unit_without_else": infer_if_type("Unit"),
}
return {
"name": "if_without_else_unit_type",
"summary": "校验省略 else 分支时隐式补全 Unit 值 () 导致的公共父类型推断规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_if_else_branch_paths(),
case_if_else_if_and_nested_conditions(),
case_if_expression_return_value(),
case_if_without_else_unit_type(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。
入口文件:projects/scala-if-else-lab/explore-if-else.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-if-else.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2420.zip