diff options
author | Li Jin <dragon-fly@qq.com> | 2025-05-28 18:20:16 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-05-28 18:20:16 +0800 |
commit | 5604bbbb80bfcedb4a9085b90864e221f8104b33 (patch) | |
tree | 8828cddb1b0fe0ca734d6209507bdbda3133f48d /doc/docs | |
parent | 87267ca9e93606b70bdc0397349b176b8d142514 (diff) | |
download | yuescript-5604bbbb80bfcedb4a9085b90864e221f8104b33.tar.gz yuescript-5604bbbb80bfcedb4a9085b90864e221f8104b33.tar.bz2 yuescript-5604bbbb80bfcedb4a9085b90864e221f8104b33.zip |
Added `try!` syntax.
Diffstat (limited to 'doc/docs')
-rwxr-xr-x | doc/docs/doc/README.md | 65 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 41 |
2 files changed, 94 insertions, 12 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index a970baa..1d9c8ad 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -1243,7 +1243,7 @@ If the destructuring statement is complicated, feel free to spread it out over a | |||
1243 | </pre> | 1243 | </pre> |
1244 | </YueDisplay> | 1244 | </YueDisplay> |
1245 | 1245 | ||
1246 | It’s common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: | 1246 | It's common to extract values from at table and assign them the local variables that have the same name as the key. In order to avoid repetition we can use the **:** prefix operator: |
1247 | 1247 | ||
1248 | ```moonscript | 1248 | ```moonscript |
1249 | {:concat, :insert} = table | 1249 | {:concat, :insert} = table |
@@ -1516,6 +1516,47 @@ catch err | |||
1516 | </pre> | 1516 | </pre> |
1517 | </YueDisplay> | 1517 | </YueDisplay> |
1518 | 1518 | ||
1519 | ### Try! | ||
1520 | |||
1521 | `try!` is a more concise error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, otherwise return nil instead of error object. | ||
1522 | |||
1523 | ```moonscript | ||
1524 | a, b, c = try! func! | ||
1525 | |||
1526 | -- with nil coalescing operator | ||
1527 | a = (try! func!) ?? "default" | ||
1528 | |||
1529 | -- as function argument | ||
1530 | f try! func! | ||
1531 | |||
1532 | -- with catch block | ||
1533 | f try! | ||
1534 | print 123 | ||
1535 | func! | ||
1536 | catch e | ||
1537 | print e | ||
1538 | e | ||
1539 | ``` | ||
1540 | <YueDisplay> | ||
1541 | <pre> | ||
1542 | a, b, c = try! func! | ||
1543 | |||
1544 | -- with nil coalescing operator | ||
1545 | a = (try! func!) ?? "default" | ||
1546 | |||
1547 | -- as function argument | ||
1548 | f try! func! | ||
1549 | |||
1550 | -- with catch block | ||
1551 | f try! | ||
1552 | print 123 | ||
1553 | func! | ||
1554 | catch e | ||
1555 | print e | ||
1556 | e | ||
1557 | </pre> | ||
1558 | </YueDisplay> | ||
1559 | |||
1519 | ## Attributes | 1560 | ## Attributes |
1520 | 1561 | ||
1521 | Syntax support for Lua 5.4 attributes. And you can still use both the `const` and `close` declaration and get constant check and scoped callback working when targeting Lua versions below 5.4. | 1562 | Syntax support for Lua 5.4 attributes. And you can still use both the `const` and `close` declaration and get constant check and scoped callback working when targeting Lua versions below 5.4. |
@@ -2417,7 +2458,7 @@ print func_b! -- prints table object | |||
2417 | </pre> | 2458 | </pre> |
2418 | </YueDisplay> | 2459 | </YueDisplay> |
2419 | 2460 | ||
2420 | This is done to avoid the needless creation of tables for functions that don’t need to return the results of the loop. | 2461 | This is done to avoid the needless creation of tables for functions that don't need to return the results of the loop. |
2421 | 2462 | ||
2422 | ## Repeat Loop | 2463 | ## Repeat Loop |
2423 | 2464 | ||
@@ -2746,7 +2787,7 @@ next_number = switch b | |||
2746 | </pre> | 2787 | </pre> |
2747 | </YueDisplay> | 2788 | </YueDisplay> |
2748 | 2789 | ||
2749 | We can use the then keyword to write a switch’s when block on a single line. No extra keyword is needed to write the else block on a single line. | 2790 | We can use the then keyword to write a switch's when block on a single line. No extra keyword is needed to write the else block on a single line. |
2750 | 2791 | ||
2751 | ```moonscript | 2792 | ```moonscript |
2752 | msg = switch math.random(1, 5) | 2793 | msg = switch math.random(1, 5) |
@@ -2792,7 +2833,7 @@ else | |||
2792 | </pre> | 2833 | </pre> |
2793 | </YueDisplay> | 2834 | </YueDisplay> |
2794 | 2835 | ||
2795 | It is worth noting the order of the case comparison expression. The case’s expression is on the left hand side. This can be useful if the case’s expression wants to overwrite how the comparison is done by defining an eq metamethod. | 2836 | It is worth noting the order of the case comparison expression. The case's expression is on the left hand side. This can be useful if the case's expression wants to overwrite how the comparison is done by defining an eq metamethod. |
2796 | 2837 | ||
2797 | ### Table Matching | 2838 | ### Table Matching |
2798 | 2839 | ||
@@ -3079,7 +3120,7 @@ class BackPack extends Inventory | |||
3079 | 3120 | ||
3080 | Here we extend our Inventory class, and limit the amount of items it can carry. | 3121 | Here we extend our Inventory class, and limit the amount of items it can carry. |
3081 | 3122 | ||
3082 | In this example, we don’t define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor. | 3123 | In this example, we don't define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor. |
3083 | 3124 | ||
3084 | Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class. | 3125 | Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class. |
3085 | 3126 | ||
@@ -3166,13 +3207,13 @@ print BackPack.size -- prints 10 | |||
3166 | 3207 | ||
3167 | The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class. | 3208 | The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class. |
3168 | 3209 | ||
3169 | The class object can be called like a function in order to create new instances. That’s how we created instances of classes in the examples above. | 3210 | The class object can be called like a function in order to create new instances. That's how we created instances of classes in the examples above. |
3170 | 3211 | ||
3171 | A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base. | 3212 | A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base. |
3172 | 3213 | ||
3173 | The class object’s metatable reads properties from the base if they don’t exist in the class object. This means we can access functions and properties directly from the class. | 3214 | The class object's metatable reads properties from the base if they don't exist in the class object. This means we can access functions and properties directly from the class. |
3174 | 3215 | ||
3175 | It is important to note that assigning to the class object does not assign into the base, so it’s not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below. | 3216 | It is important to note that assigning to the class object does not assign into the base, so it's not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below. |
3176 | 3217 | ||
3177 | The class object has a couple special properties: | 3218 | The class object has a couple special properties: |
3178 | 3219 | ||
@@ -3245,7 +3286,7 @@ print Counter.count -- prints 2 | |||
3245 | </pre> | 3286 | </pre> |
3246 | </YueDisplay> | 3287 | </YueDisplay> |
3247 | 3288 | ||
3248 | The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua’s colon syntax. | 3289 | The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua's colon syntax. |
3249 | 3290 | ||
3250 | ```moonscript | 3291 | ```moonscript |
3251 | @@hello 1,2,3,4 | 3292 | @@hello 1,2,3,4 |
@@ -3260,7 +3301,7 @@ The calling semantics of @@ are similar to @. Calling a @@ name will pass the cl | |||
3260 | 3301 | ||
3261 | In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object. | 3302 | In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object. |
3262 | 3303 | ||
3263 | Here is an alternative way to create a class variable compared to what’s described above: | 3304 | Here is an alternative way to create a class variable compared to what's described above: |
3264 | 3305 | ||
3265 | ```moonscript | 3306 | ```moonscript |
3266 | class Things | 3307 | class Things |
@@ -3593,7 +3634,7 @@ print var -- nil here | |||
3593 | </pre> | 3634 | </pre> |
3594 | </YueDisplay> | 3635 | </YueDisplay> |
3595 | 3636 | ||
3596 | YueScript’s **do** can also be used an expression . Allowing you to combine multiple lines into one. The result of the do expression is the last statement in its body. | 3637 | YueScript's **do** can also be used an expression . Allowing you to combine multiple lines into one. The result of the do expression is the last statement in its body. |
3597 | 3638 | ||
3598 | ```moonscript | 3639 | ```moonscript |
3599 | counter = do | 3640 | counter = do |
@@ -3719,7 +3760,7 @@ print i -- will print 0 | |||
3719 | </pre> | 3760 | </pre> |
3720 | </YueDisplay> | 3761 | </YueDisplay> |
3721 | 3762 | ||
3722 | In my_func, we've overwritten the value of i mistakenly. In this example it is quite obvious, but consider a large, or foreign code base where it isn’t clear what names have already been declared. | 3763 | In my_func, we've overwritten the value of i mistakenly. In this example it is quite obvious, but consider a large, or foreign code base where it isn't clear what names have already been declared. |
3723 | 3764 | ||
3724 | It would be helpful to say which variables from the enclosing scope we intend on change, in order to prevent us from changing others by accident. | 3765 | It would be helpful to say which variables from the enclosing scope we intend on change, in order to prevent us from changing others by accident. |
3725 | 3766 | ||
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 2d4af18..0fa1fed 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -1514,6 +1514,47 @@ catch err | |||
1514 | </pre> | 1514 | </pre> |
1515 | </YueDisplay> | 1515 | </YueDisplay> |
1516 | 1516 | ||
1517 | ### 错误处理简化 | ||
1518 | |||
1519 | `try!` 是 `try` 的简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。 | ||
1520 | |||
1521 | ```moonscript | ||
1522 | a, b, c = try! func! | ||
1523 | |||
1524 | -- 与空值合并运算符一起使用 | ||
1525 | a = (try! func!) ?? "default" | ||
1526 | |||
1527 | -- 作为函数参数 | ||
1528 | f try! func! | ||
1529 | |||
1530 | -- 带 catch 块的 try! | ||
1531 | f try! | ||
1532 | print 123 | ||
1533 | func! | ||
1534 | catch e | ||
1535 | print e | ||
1536 | e | ||
1537 | ``` | ||
1538 | <YueDisplay> | ||
1539 | <pre> | ||
1540 | a, b, c = try! func! | ||
1541 | |||
1542 | -- 与空值合并运算符一起使用 | ||
1543 | a = (try! func!) ?? "default" | ||
1544 | |||
1545 | -- 作为函数参数 | ||
1546 | f try! func! | ||
1547 | |||
1548 | -- 带 catch 块的 try! | ||
1549 | f try! | ||
1550 | print 123 | ||
1551 | func! | ||
1552 | catch e | ||
1553 | print e | ||
1554 | e | ||
1555 | </pre> | ||
1556 | </YueDisplay> | ||
1557 | |||
1517 | ## 属性 | 1558 | ## 属性 |
1518 | 1559 | ||
1519 | 月之脚本现在提供了Lua 5.4新增的叫做属性的语法支持。在月之脚本编译到的Lua目标版本低于5.4时,你仍然可以同时使用`const`和`close`的属性声明语法,并获得常量检查和作用域回调的功能。 | 1560 | 月之脚本现在提供了Lua 5.4新增的叫做属性的语法支持。在月之脚本编译到的Lua目标版本低于5.4时,你仍然可以同时使用`const`和`close`的属性声明语法,并获得常量检查和作用域回调的功能。 |