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/doc/README.md | 22 +++++++++++----------- doc/docs/zh/doc/README.md | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'doc') 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 ## If Assignment -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. +`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. ```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 "You have hello", hello -elseif world = os.getenv "world" +elseif world := os.getenv "world" print "you have world", world else print "nothing :(" ```
-if hello = os.getenv "hello"
+if hello := os.getenv "hello"
   print "You have hello", hello
-elseif world = os.getenv "world"
+elseif world := os.getenv "world"
   print "you have world", world
 else
   print "nothing :("
@@ -1259,13 +1259,13 @@ else
 
 If assignment with multiple return values. Only the first value is getting checked, other values are scoped.
 ```moonscript
-if success, result = pcall -> "get result without problems"
+if success, result := pcall -> "get result without problems"
   print result -- variable result is scoped
 print "OK"
 ```
 
 
-if success, result = pcall -> "get result without problems"
+if success, result := pcall -> "get result without problems"
   print result -- variable result is scoped
 print "OK"
 
@@ -1374,7 +1374,7 @@ try func 1, 2, 3 -- working with if assignment pattern -if success, result = try func 1, 2, 3 +if success, result := try func 1, 2, 3 catch err print yue.traceback err print result @@ -1402,7 +1402,7 @@ try func 1, 2, 3 -- working with if assignment pattern -if success, result = try func 1, 2, 3 +if success, result := try func 1, 2, 3 catch err print yue.traceback err 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 ## 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