From 514b9f97febe8920a78d6078b092fe84b859a963 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 7 Dec 2023 23:49:48 +0800 Subject: changed the if-assignment syntax to prevent some errors. --- doc/docs/zh/doc/README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'doc/docs/zh') 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 ## If 赋值 -`if` 和 `elseif` 代码块可以在条件表达式的位置进行赋值。在代码执行到要计算条件时,会首先进行赋值计算,并使用赋与的值作为分支判断的条件。赋值的变量仅在条件分支的代码块内有效,这意味着如果值不是真值,那么它就不会被用到。 +`if` 和 `elseif` 代码块可以在条件表达式的位置进行赋值。在代码执行到要计算条件时,会首先进行赋值计算,并使用赋与的值作为分支判断的条件。赋值的变量仅在条件分支的代码块内有效,这意味着如果值不是真值,那么它就不会被用到。注意,你必须使用“海象运算符” `:=` 而不是 `=` 来做赋值。 ```moonscript -if user = database.find_user "moon" +if user := database.find_user "moon" print user.name ```
-if user = database.find_user "moon"
+if user := database.find_user "moon"
   print user.name
 
```moonscript -if hello = os.getenv "hello" +if hello := os.getenv "hello" print "你有 hello", hello -elseif world = os.getenv "world" +elseif world := os.getenv "world" print "你有 world", world else print "什么都没有 :(" ```
-if hello = os.getenv "hello"
+if hello := os.getenv "hello"
   print "你有 hello", hello
-elseif world = os.getenv "world"
+elseif world := os.getenv "world"
   print "你有 world", world
 else
   print "什么都没有 :("
@@ -1257,13 +1257,13 @@ else
 
 使用多个返回值的 If 赋值。只有第一个值会被检查,其他值都有同样的作用域。
 ```moonscript
-if success, result = pcall -> "无报错地获取结果"
+if success, result := pcall -> "无报错地获取结果"
   print result -- 变量 result 是有作用域的
 print "好的"
 ```
 
 
-if success, result = pcall -> "无报错地获取结果"
+if success, result := pcall -> "无报错地获取结果"
   print result -- 变量 result 是有作用域的
 print "好的"
 
@@ -1372,7 +1372,7 @@ try func 1, 2, 3 -- 使用if赋值模式 -if success, result = try func 1, 2, 3 +if success, result := try func 1, 2, 3 catch err print yue.traceback err print result @@ -1400,7 +1400,7 @@ try func 1, 2, 3 -- 使用if赋值模式 -if success, result = try func 1, 2, 3 +if success, result := try func 1, 2, 3 catch err print yue.traceback err print result -- cgit v1.2.3-55-g6feb