diff options
author | Li Jin <dragon-fly@qq.com> | 2023-12-07 23:49:48 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-12-07 23:55:16 +0800 |
commit | 514b9f97febe8920a78d6078b092fe84b859a963 (patch) | |
tree | 8c76ba7579f69db7e2c899e4713009b910e0fa89 /doc | |
parent | a1d719e3bbfe8cd39c05d2a8f49143b9e814f876 (diff) | |
download | yuescript-514b9f97febe8920a78d6078b092fe84b859a963.tar.gz yuescript-514b9f97febe8920a78d6078b092fe84b859a963.tar.bz2 yuescript-514b9f97febe8920a78d6078b092fe84b859a963.zip |
changed the if-assignment syntax to prevent some errors.v0.21.0
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/docs/doc/README.md | 22 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 22 |
2 files changed, 22 insertions, 22 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 54c6b72..041120b 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -1225,32 +1225,32 @@ We know each element in the array table is a two item tuple, so we can unpack it | |||
1225 | 1225 | ||
1226 | ## If Assignment | 1226 | ## If Assignment |
1227 | 1227 | ||
1228 | if and elseif blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. | 1228 | `if` and `elseif` blocks can take an assignment in place of a conditional expression. Upon evaluating the conditional, the assignment will take place and the value that was assigned to will be used as the conditional expression. The assigned variable is only in scope for the body of the conditional, meaning it is never available if the value is not truthy. And you have to use "the walrus operator" `:=` instead of `=` to do assignment. |
1229 | 1229 | ||
1230 | ```moonscript | 1230 | ```moonscript |
1231 | if user = database.find_user "moon" | 1231 | if user := database.find_user "moon" |
1232 | print user.name | 1232 | print user.name |
1233 | ``` | 1233 | ``` |
1234 | <YueDisplay> | 1234 | <YueDisplay> |
1235 | <pre> | 1235 | <pre> |
1236 | if user = database.find_user "moon" | 1236 | if user := database.find_user "moon" |
1237 | print user.name | 1237 | print user.name |
1238 | </pre> | 1238 | </pre> |
1239 | </YueDisplay> | 1239 | </YueDisplay> |
1240 | 1240 | ||
1241 | ```moonscript | 1241 | ```moonscript |
1242 | if hello = os.getenv "hello" | 1242 | if hello := os.getenv "hello" |
1243 | print "You have hello", hello | 1243 | print "You have hello", hello |
1244 | elseif world = os.getenv "world" | 1244 | elseif world := os.getenv "world" |
1245 | print "you have world", world | 1245 | print "you have world", world |
1246 | else | 1246 | else |
1247 | print "nothing :(" | 1247 | print "nothing :(" |
1248 | ``` | 1248 | ``` |
1249 | <YueDisplay> | 1249 | <YueDisplay> |
1250 | <pre> | 1250 | <pre> |
1251 | if hello = os.getenv "hello" | 1251 | if hello := os.getenv "hello" |
1252 | print "You have hello", hello | 1252 | print "You have hello", hello |
1253 | elseif world = os.getenv "world" | 1253 | elseif world := os.getenv "world" |
1254 | print "you have world", world | 1254 | print "you have world", world |
1255 | else | 1255 | else |
1256 | print "nothing :(" | 1256 | print "nothing :(" |
@@ -1259,13 +1259,13 @@ else | |||
1259 | 1259 | ||
1260 | If assignment with multiple return values. Only the first value is getting checked, other values are scoped. | 1260 | If assignment with multiple return values. Only the first value is getting checked, other values are scoped. |
1261 | ```moonscript | 1261 | ```moonscript |
1262 | if success, result = pcall -> "get result without problems" | 1262 | if success, result := pcall -> "get result without problems" |
1263 | print result -- variable result is scoped | 1263 | print result -- variable result is scoped |
1264 | print "OK" | 1264 | print "OK" |
1265 | ``` | 1265 | ``` |
1266 | <YueDisplay> | 1266 | <YueDisplay> |
1267 | <pre> | 1267 | <pre> |
1268 | if success, result = pcall -> "get result without problems" | 1268 | if success, result := pcall -> "get result without problems" |
1269 | print result -- variable result is scoped | 1269 | print result -- variable result is scoped |
1270 | print "OK" | 1270 | print "OK" |
1271 | </pre> | 1271 | </pre> |
@@ -1374,7 +1374,7 @@ try | |||
1374 | func 1, 2, 3 | 1374 | func 1, 2, 3 |
1375 | 1375 | ||
1376 | -- working with if assignment pattern | 1376 | -- working with if assignment pattern |
1377 | if success, result = try func 1, 2, 3 | 1377 | if success, result := try func 1, 2, 3 |
1378 | catch err | 1378 | catch err |
1379 | print yue.traceback err | 1379 | print yue.traceback err |
1380 | print result | 1380 | print result |
@@ -1402,7 +1402,7 @@ try | |||
1402 | func 1, 2, 3 | 1402 | func 1, 2, 3 |
1403 | 1403 | ||
1404 | -- working with if assignment pattern | 1404 | -- working with if assignment pattern |
1405 | if success, result = try func 1, 2, 3 | 1405 | if success, result := try func 1, 2, 3 |
1406 | catch err | 1406 | catch err |
1407 | print yue.traceback err | 1407 | print yue.traceback err |
1408 | print result | 1408 | print result |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 7e17b80..a856e7b 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -1223,32 +1223,32 @@ for [left, right] in *tuples | |||
1223 | 1223 | ||
1224 | ## If 赋值 | 1224 | ## If 赋值 |
1225 | 1225 | ||
1226 | `if` 和 `elseif` 代码块可以在条件表达式的位置进行赋值。在代码执行到要计算条件时,会首先进行赋值计算,并使用赋与的值作为分支判断的条件。赋值的变量仅在条件分支的代码块内有效,这意味着如果值不是真值,那么它就不会被用到。 | 1226 | `if` 和 `elseif` 代码块可以在条件表达式的位置进行赋值。在代码执行到要计算条件时,会首先进行赋值计算,并使用赋与的值作为分支判断的条件。赋值的变量仅在条件分支的代码块内有效,这意味着如果值不是真值,那么它就不会被用到。注意,你必须使用“海象运算符” `:=` 而不是 `=` 来做赋值。 |
1227 | 1227 | ||
1228 | ```moonscript | 1228 | ```moonscript |
1229 | if user = database.find_user "moon" | 1229 | if user := database.find_user "moon" |
1230 | print user.name | 1230 | print user.name |
1231 | ``` | 1231 | ``` |
1232 | <YueDisplay> | 1232 | <YueDisplay> |
1233 | <pre> | 1233 | <pre> |
1234 | if user = database.find_user "moon" | 1234 | if user := database.find_user "moon" |
1235 | print user.name | 1235 | print user.name |
1236 | </pre> | 1236 | </pre> |
1237 | </YueDisplay> | 1237 | </YueDisplay> |
1238 | 1238 | ||
1239 | ```moonscript | 1239 | ```moonscript |
1240 | if hello = os.getenv "hello" | 1240 | if hello := os.getenv "hello" |
1241 | print "你有 hello", hello | 1241 | print "你有 hello", hello |
1242 | elseif world = os.getenv "world" | 1242 | elseif world := os.getenv "world" |
1243 | print "你有 world", world | 1243 | print "你有 world", world |
1244 | else | 1244 | else |
1245 | print "什么都没有 :(" | 1245 | print "什么都没有 :(" |
1246 | ``` | 1246 | ``` |
1247 | <YueDisplay> | 1247 | <YueDisplay> |
1248 | <pre> | 1248 | <pre> |
1249 | if hello = os.getenv "hello" | 1249 | if hello := os.getenv "hello" |
1250 | print "你有 hello", hello | 1250 | print "你有 hello", hello |
1251 | elseif world = os.getenv "world" | 1251 | elseif world := os.getenv "world" |
1252 | print "你有 world", world | 1252 | print "你有 world", world |
1253 | else | 1253 | else |
1254 | print "什么都没有 :(" | 1254 | print "什么都没有 :(" |
@@ -1257,13 +1257,13 @@ else | |||
1257 | 1257 | ||
1258 | 使用多个返回值的 If 赋值。只有第一个值会被检查,其他值都有同样的作用域。 | 1258 | 使用多个返回值的 If 赋值。只有第一个值会被检查,其他值都有同样的作用域。 |
1259 | ```moonscript | 1259 | ```moonscript |
1260 | if success, result = pcall -> "无报错地获取结果" | 1260 | if success, result := pcall -> "无报错地获取结果" |
1261 | print result -- 变量 result 是有作用域的 | 1261 | print result -- 变量 result 是有作用域的 |
1262 | print "好的" | 1262 | print "好的" |
1263 | ``` | 1263 | ``` |
1264 | <YueDisplay> | 1264 | <YueDisplay> |
1265 | <pre> | 1265 | <pre> |
1266 | if success, result = pcall -> "无报错地获取结果" | 1266 | if success, result := pcall -> "无报错地获取结果" |
1267 | print result -- 变量 result 是有作用域的 | 1267 | print result -- 变量 result 是有作用域的 |
1268 | print "好的" | 1268 | print "好的" |
1269 | </pre> | 1269 | </pre> |
@@ -1372,7 +1372,7 @@ try | |||
1372 | func 1, 2, 3 | 1372 | func 1, 2, 3 |
1373 | 1373 | ||
1374 | -- 使用if赋值模式 | 1374 | -- 使用if赋值模式 |
1375 | if success, result = try func 1, 2, 3 | 1375 | if success, result := try func 1, 2, 3 |
1376 | catch err | 1376 | catch err |
1377 | print yue.traceback err | 1377 | print yue.traceback err |
1378 | print result | 1378 | print result |
@@ -1400,7 +1400,7 @@ try | |||
1400 | func 1, 2, 3 | 1400 | func 1, 2, 3 |
1401 | 1401 | ||
1402 | -- 使用if赋值模式 | 1402 | -- 使用if赋值模式 |
1403 | if success, result = try func 1, 2, 3 | 1403 | if success, result := try func 1, 2, 3 |
1404 | catch err | 1404 | catch err |
1405 | print yue.traceback err | 1405 | print yue.traceback err |
1406 | print result | 1406 | print result |