Skip to content

Commit 5604bbb

Browse files
committed
Added try! syntax.
1 parent 87267ca commit 5604bbb

22 files changed

Lines changed: 1169 additions & 189 deletions

doc/docs/doc/README.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ If the destructuring statement is complicated, feel free to spread it out over a
12431243
</pre>
12441244
</YueDisplay>
12451245

1246-
Its 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:
12471247

12481248
```moonscript
12491249
{:concat, :insert} = table
@@ -1516,6 +1516,47 @@ catch err
15161516
</pre>
15171517
</YueDisplay>
15181518

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+
15191560
## Attributes
15201561

15211562
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
24172458
</pre>
24182459
</YueDisplay>
24192460

2420-
This is done to avoid the needless creation of tables for functions that dont 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.
24212462

24222463
## Repeat Loop
24232464

@@ -2746,7 +2787,7 @@ next_number = switch b
27462787
</pre>
27472788
</YueDisplay>
27482789

2749-
We can use the then keyword to write a switchs 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.
27502791

27512792
```moonscript
27522793
msg = switch math.random(1, 5)
@@ -2792,7 +2833,7 @@ else
27922833
</pre>
27932834
</YueDisplay>
27942835

2795-
It is worth noting the order of the case comparison expression. The cases expression is on the left hand side. This can be useful if the cases 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.
27962837

27972838
### Table Matching
27982839

@@ -3079,7 +3120,7 @@ class BackPack extends Inventory
30793120

30803121
Here we extend our Inventory class, and limit the amount of items it can carry.
30813122

3082-
In this example, we dont 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.
30833124

30843125
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.
30853126

@@ -3166,13 +3207,13 @@ print BackPack.size -- prints 10
31663207

31673208
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.
31683209

3169-
The class object can be called like a function in order to create new instances. Thats 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.
31703211

31713212
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.
31723213

3173-
The class objects metatable reads properties from the base if they dont 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.
31743215

3175-
It is important to note that assigning to the class object does not assign into the base, so its 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.
31763217

31773218
The class object has a couple special properties:
31783219

@@ -3245,7 +3286,7 @@ print Counter.count -- prints 2
32453286
</pre>
32463287
</YueDisplay>
32473288

3248-
The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Luas 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.
32493290

32503291
```moonscript
32513292
@@hello 1,2,3,4
@@ -3260,7 +3301,7 @@ The calling semantics of @@ are similar to @. Calling a @@ name will pass the cl
32603301

32613302
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.
32623303

3263-
Here is an alternative way to create a class variable compared to whats described above:
3304+
Here is an alternative way to create a class variable compared to what's described above:
32643305

32653306
```moonscript
32663307
class Things
@@ -3593,7 +3634,7 @@ print var -- nil here
35933634
</pre>
35943635
</YueDisplay>
35953636

3596-
YueScripts **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.
35973638

35983639
```moonscript
35993640
counter = do
@@ -3719,7 +3760,7 @@ print i -- will print 0
37193760
</pre>
37203761
</YueDisplay>
37213762

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 isnt 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.
37233764

37243765
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.
37253766

doc/docs/zh/doc/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,47 @@ catch err
15141514
</pre>
15151515
</YueDisplay>
15161516

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+
15171558
## 属性
15181559

15191560
月之脚本现在提供了Lua 5.4新增的叫做属性的语法支持。在月之脚本编译到的Lua目标版本低于5.4时,你仍然可以同时使用`const``close`的属性声明语法,并获得常量检查和作用域回调的功能。

spec/inputs/try_catch.yue

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,126 @@ f = ->
6262
do
6363
<- x
6464
local tb, a, b, c
65-
f = -> try tb.f a, b, c
65+
f1 = -> try tb.f a, b, c
66+
67+
do
68+
f1 = -> do
69+
ok, ... = try func!
70+
...
71+
72+
do
73+
local func
74+
a, b, c = try! func!
75+
76+
do
77+
a, b, c = try! func!
78+
79+
do
80+
a = (try! func!) ?? "default"
81+
82+
do
83+
f try! func!
84+
85+
do
86+
f try!
87+
print 123
88+
func!
89+
catch e
90+
print e
91+
e
6692

6793
nil
94+
95+
do
96+
try
97+
func 1, 2, 3
98+
catch err
99+
print err
100+
101+
try func 1, 2, 3
102+
catch err
103+
print err
104+
105+
try
106+
print "trying"
107+
func 1, 2, 3
108+
109+
do
110+
success, result = try
111+
func 1, 2, 3
112+
catch err
113+
print err
114+
115+
success, result = try func 1, 2, 3
116+
117+
tb = {}
118+
119+
try tb.func
120+
try tb.func!
121+
try tb.func()
122+
try (tb.func!)
123+
try (tb\func(1, 2, 3))
124+
125+
try tb.func 1
126+
try tb.func(1)
127+
128+
if (try func 1
129+
catch err
130+
print err)
131+
print "OK"
132+
133+
if try (func 1)
134+
catch err
135+
print err
136+
print "OK"
137+
138+
do
139+
if success, result := try func "abc", 123
140+
print result
141+
142+
success, result = try func "abc", 123
143+
catch err
144+
print err
145+
146+
print result if success, result := try func "abc", 123
147+
catch err
148+
print err
149+
150+
do
151+
try
152+
func 1, 2, 3
153+
154+
try func 1, 2, 3
155+
156+
do
157+
<- x
158+
local tb, a, b, c
159+
f1 = -> try tb.f a, b, c
160+
161+
do
162+
f1 = -> do
163+
ok, ... = try func!
164+
...
165+
166+
do
167+
local func
168+
a, b, c = try! func!
169+
170+
do
171+
a, b, c = try! func!
172+
173+
do
174+
a = (try! func!) ?? "default"
175+
176+
do
177+
f try! func!
178+
179+
do
180+
f try!
181+
print 123
182+
func!
183+
catch e
184+
print e
185+
e
186+
187+
nil

0 commit comments

Comments
 (0)