You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/docs/doc/README.md
+53-12Lines changed: 53 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1243,7 +1243,7 @@ If the destructuring statement is complicated, feel free to spread it out over a
1243
1243
</pre>
1244
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
1248
```moonscript
1249
1249
{:concat, :insert} = table
@@ -1516,6 +1516,47 @@ catch err
1516
1516
</pre>
1517
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
1560
## Attributes
1520
1561
1521
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
2458
</pre>
2418
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
2463
## Repeat Loop
2423
2464
@@ -2746,7 +2787,7 @@ next_number = switch b
2746
2787
</pre>
2747
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
2792
```moonscript
2752
2793
msg = switch math.random(1, 5)
@@ -2792,7 +2833,7 @@ else
2792
2833
</pre>
2793
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
2838
### Table Matching
2798
2839
@@ -3079,7 +3120,7 @@ class BackPack extends Inventory
3079
3120
3080
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
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
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
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
3218
The class object has a couple special properties:
3178
3219
@@ -3245,7 +3286,7 @@ print Counter.count -- prints 2
3245
3286
</pre>
3246
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
3291
```moonscript
3251
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
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
3306
```moonscript
3266
3307
class Things
@@ -3593,7 +3634,7 @@ print var -- nil here
3593
3634
</pre>
3594
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
3639
```moonscript
3599
3640
counter = do
@@ -3719,7 +3760,7 @@ print i -- will print 0
3719
3760
</pre>
3720
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
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.
0 commit comments