diff options
Diffstat (limited to 'doc/docs/zh/doc/switch.md')
| -rw-r--r-- | doc/docs/zh/doc/switch.md | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/doc/docs/zh/doc/switch.md b/doc/docs/zh/doc/switch.md new file mode 100644 index 0000000..700bc2a --- /dev/null +++ b/doc/docs/zh/doc/switch.md | |||
| @@ -0,0 +1,296 @@ | |||
| 1 | # switch 语句 | ||
| 2 | |||
| 3 |   switch 语句是为了简化检查一系列相同值的if语句而提供的简写语法。要注意用于比较检查的目标值只会计算一次。和 if 语句一样,switch 语句在最后可以接一个 else 代码块来处理没有匹配的情况。在生成的 Lua 代码中,进行比较是使用 == 操作符完成的。switch 语句中也可以使用赋值表达式来储存临时变量值。 | ||
| 4 | |||
| 5 | ```yuescript | ||
| 6 | switch name := "Dan" | ||
| 7 | when "Robert" | ||
| 8 | print "你是Robert" | ||
| 9 | when "Dan", "Daniel" | ||
| 10 | print "你的名字是Dan" | ||
| 11 | else | ||
| 12 | print "我不认识你,你的名字是#{name}" | ||
| 13 | ``` | ||
| 14 | <YueDisplay> | ||
| 15 | |||
| 16 | ```yue | ||
| 17 | switch name := "Dan" | ||
| 18 | when "Robert" | ||
| 19 | print "你是Robert" | ||
| 20 | when "Dan", "Daniel" | ||
| 21 | print "你的名字是Dan" | ||
| 22 | else | ||
| 23 | print "我不认识你,你的名字是#{name}" | ||
| 24 | ``` | ||
| 25 | |||
| 26 | </YueDisplay> | ||
| 27 | |||
| 28 |   switch 语句的 when 子句中可以通过使用逗号分隔的列表来匹配多个值。 | ||
| 29 | |||
| 30 |   switch 语句也可以作为表达式使用,下面我们可以将 switch 语句返回的结果分配给一个变量: | ||
| 31 | |||
| 32 | ```yuescript | ||
| 33 | b = 1 | ||
| 34 | next_number = switch b | ||
| 35 | when 1 | ||
| 36 | 2 | ||
| 37 | when 2 | ||
| 38 | 3 | ||
| 39 | else | ||
| 40 | error "数字数得太大了!" | ||
| 41 | ``` | ||
| 42 | <YueDisplay> | ||
| 43 | |||
| 44 | ```yue | ||
| 45 | b = 1 | ||
| 46 | next_number = switch b | ||
| 47 | when 1 | ||
| 48 | 2 | ||
| 49 | when 2 | ||
| 50 | 3 | ||
| 51 | else | ||
| 52 | error "数字数得太大了!" | ||
| 53 | ``` | ||
| 54 | |||
| 55 | </YueDisplay> | ||
| 56 | |||
| 57 |   我们可以使用 then 关键字在 when 子句的同一行上编写处理代码。else 代码块的后续代码中要写在同一行上不需要额外的关键字。 | ||
| 58 | |||
| 59 | ```yuescript | ||
| 60 | msg = switch math.random(1, 5) | ||
| 61 | when 1 then "你很幸运" | ||
| 62 | when 2 then "你差点很幸运" | ||
| 63 | else "不太幸运" | ||
| 64 | ``` | ||
| 65 | <YueDisplay> | ||
| 66 | |||
| 67 | ```yue | ||
| 68 | msg = switch math.random(1, 5) | ||
| 69 | when 1 then "你很幸运" | ||
| 70 | when 2 then "你差点很幸运" | ||
| 71 | else "不太幸运" | ||
| 72 | ``` | ||
| 73 | |||
| 74 | </YueDisplay> | ||
| 75 | |||
| 76 |   如果在编写 switch 语句时希望少写一个缩进,那么你可以把第一个 when 子句放在 switch 开始语句的第一行,然后后续的子语句就都可以都少写一个缩进。 | ||
| 77 | |||
| 78 | ```yuescript | ||
| 79 | switch math.random(1, 5) | ||
| 80 | when 1 | ||
| 81 | print "你很幸运" -- 两个缩进级别 | ||
| 82 | else | ||
| 83 | print "不太幸运" | ||
| 84 | |||
| 85 | switch math.random(1, 5) when 1 | ||
| 86 | print "你很幸运" -- 一个缩进级别 | ||
| 87 | else | ||
| 88 | print "不太幸运" | ||
| 89 | ``` | ||
| 90 | <YueDisplay> | ||
| 91 | |||
| 92 | ```yue | ||
| 93 | switch math.random(1, 5) | ||
| 94 | when 1 | ||
| 95 | print "你很幸运" -- 两个缩进级别 | ||
| 96 | else | ||
| 97 | print "不太幸运" | ||
| 98 | |||
| 99 | switch math.random(1, 5) when 1 | ||
| 100 | print "你很幸运" -- 一个缩进级别 | ||
| 101 | else | ||
| 102 | print "不太幸运" | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 |   值得注意的是,在生成 Lua 代码时,我们要做检查的目标变量会放在 == 表达式的右侧。当你希望给 when 子句的比较对象定义一个 \_\_eq 元方法来重载判断逻辑时,可能会有用。 | ||
| 108 | |||
| 109 | ## 表格匹配 | ||
| 110 | |||
| 111 |   在 switch 的 when 子句中,如果期待检查目标是一个表格,且可以通过特定的结构进行解构并获得非 nil 值,那么你可以尝试使用表格匹配的语法。 | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | items = | ||
| 115 | * x: 100 | ||
| 116 | y: 200 | ||
| 117 | * width: 300 | ||
| 118 | height: 400 | ||
| 119 | |||
| 120 | for item in *items | ||
| 121 | switch item | ||
| 122 | when :x, :y | ||
| 123 | print "Vec2 #{x}, #{y}" | ||
| 124 | when :width, :height | ||
| 125 | print "尺寸 #{width}, #{height}" | ||
| 126 | ``` | ||
| 127 | <YueDisplay> | ||
| 128 | |||
| 129 | ```yue | ||
| 130 | items = | ||
| 131 | * x: 100 | ||
| 132 | y: 200 | ||
| 133 | * width: 300 | ||
| 134 | height: 400 | ||
| 135 | |||
| 136 | for item in *items | ||
| 137 | switch item | ||
| 138 | when :x, :y | ||
| 139 | print "Vec2 #{x}, #{y}" | ||
| 140 | when :width, :height | ||
| 141 | print "尺寸 #{width}, #{height}" | ||
| 142 | ``` | ||
| 143 | |||
| 144 | </YueDisplay> | ||
| 145 | |||
| 146 |   你可以使用默认值来选择性地解构表格的某些字段。 | ||
| 147 | |||
| 148 | ```yuescript | ||
| 149 | item = {} | ||
| 150 | |||
| 151 | {pos: {:x = 50, :y = 200}} = item -- 获取错误:尝试索引nil值(字段'pos') | ||
| 152 | |||
| 153 | switch item | ||
| 154 | when {pos: {:x = 50, :y = 200}} | ||
| 155 | print "Vec2 #{x}, #{y}" -- 表格解构仍然会通过 | ||
| 156 | ``` | ||
| 157 | <YueDisplay> | ||
| 158 | |||
| 159 | ```yue | ||
| 160 | item = {} | ||
| 161 | |||
| 162 | {pos: {:x = 50, :y = 200}} = item -- 获取错误:尝试索引nil值(字段'pos') | ||
| 163 | |||
| 164 | switch item | ||
| 165 | when {pos: {:x = 50, :y = 200}} | ||
| 166 | print "Vec2 #{x}, #{y}" -- 表格解构仍然会通过 | ||
| 167 | ``` | ||
| 168 | |||
| 169 | </YueDisplay> | ||
| 170 | |||
| 171 |   你也可以匹配数组元素、表格字段,甚至使用数组或表格字面量来匹配嵌套的结构。 | ||
| 172 | |||
| 173 |   匹配数组元素。 | ||
| 174 | |||
| 175 | ```yuescript | ||
| 176 | switch tb | ||
| 177 | when [1, 2, 3] | ||
| 178 | print "1, 2, 3" | ||
| 179 | when [1, b, 3] | ||
| 180 | print "1, #{b}, 3" | ||
| 181 | when [1, 2, b = 3] -- 变量b有默认值 | ||
| 182 | print "1, 2, #{b}" | ||
| 183 | ``` | ||
| 184 | <YueDisplay> | ||
| 185 | |||
| 186 | ```yue | ||
| 187 | switch tb | ||
| 188 | when [1, 2, 3] | ||
| 189 | print "1, 2, 3" | ||
| 190 | when [1, b, 3] | ||
| 191 | print "1, #{b}, 3" | ||
| 192 | when [1, 2, b = 3] -- 变量b有默认值 | ||
| 193 | print "1, 2, #{b}" | ||
| 194 | ``` | ||
| 195 | |||
| 196 | </YueDisplay> | ||
| 197 | |||
| 198 |   匹配表格字段。 | ||
| 199 | |||
| 200 | ```yuescript | ||
| 201 | switch tb | ||
| 202 | when success: true, :result | ||
| 203 | print "成功", result | ||
| 204 | when success: false | ||
| 205 | print "失败", result | ||
| 206 | else | ||
| 207 | print "无效值" | ||
| 208 | ``` | ||
| 209 | <YueDisplay> | ||
| 210 | |||
| 211 | ```yue | ||
| 212 | switch tb | ||
| 213 | when success: true, :result | ||
| 214 | print "成功", result | ||
| 215 | when success: false | ||
| 216 | print "失败", result | ||
| 217 | else | ||
| 218 | print "无效值" | ||
| 219 | ``` | ||
| 220 | |||
| 221 | </YueDisplay> | ||
| 222 | |||
| 223 |   匹配嵌套的表格结构。 | ||
| 224 | |||
| 225 | ```yuescript | ||
| 226 | switch tb | ||
| 227 | when data: {type: "success", :content} | ||
| 228 | print "成功", content | ||
| 229 | when data: {type: "error", :content} | ||
| 230 | print "失败", content | ||
| 231 | else | ||
| 232 | print "无效值" | ||
| 233 | ``` | ||
| 234 | <YueDisplay> | ||
| 235 | |||
| 236 | ```yue | ||
| 237 | switch tb | ||
| 238 | when data: {type: "success", :content} | ||
| 239 | print "成功", content | ||
| 240 | when data: {type: "error", :content} | ||
| 241 | print "失败", content | ||
| 242 | else | ||
| 243 | print "无效值" | ||
| 244 | ``` | ||
| 245 | |||
| 246 | </YueDisplay> | ||
| 247 | |||
| 248 |   匹配表格数组。 | ||
| 249 | |||
| 250 | ```yuescript | ||
| 251 | switch tb | ||
| 252 | when [ | ||
| 253 | {a: 1, b: 2} | ||
| 254 | {a: 3, b: 4} | ||
| 255 | {a: 5, b: 6} | ||
| 256 | fourth | ||
| 257 | ] | ||
| 258 | print "匹配成功", fourth | ||
| 259 | ``` | ||
| 260 | <YueDisplay> | ||
| 261 | |||
| 262 | ```yue | ||
| 263 | switch tb | ||
| 264 | when [ | ||
| 265 | {a: 1, b: 2} | ||
| 266 | {a: 3, b: 4} | ||
| 267 | {a: 5, b: 6} | ||
| 268 | fourth | ||
| 269 | ] | ||
| 270 | print "匹配成功", fourth | ||
| 271 | ``` | ||
| 272 | |||
| 273 | </YueDisplay> | ||
| 274 | |||
| 275 |   匹配一个列表并捕获特定范围内的元素。 | ||
| 276 | |||
| 277 | ```yuescript | ||
| 278 | segments = ["admin", "users", "logs", "view"] | ||
| 279 | switch segments | ||
| 280 | when [...groups, resource, action] | ||
| 281 | print "Group:", groups -- 打印: {"admin", "users"} | ||
| 282 | print "Resource:", resource -- 打印: "logs" | ||
| 283 | print "Action:", action -- 打印: "view" | ||
| 284 | ``` | ||
| 285 | <YueDisplay> | ||
| 286 | |||
| 287 | ```yue | ||
| 288 | segments = ["admin", "users", "logs", "view"] | ||
| 289 | switch segments | ||
| 290 | when [...groups, resource, action] | ||
| 291 | print "Group:", groups -- 打印: {"admin", "users"} | ||
| 292 | print "Resource:", resource -- 打印: "logs" | ||
| 293 | print "Action:", action -- 打印: "view" | ||
| 294 | ``` | ||
| 295 | |||
| 296 | </YueDisplay> | ||
