Skip to content

Commit 2f61682

Browse files
committed
Replace try! with try?.
1 parent 5604bbb commit 2f61682

8 files changed

Lines changed: 38 additions & 46 deletions

File tree

doc/docs/doc/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,21 +1516,21 @@ catch err
15161516
</pre>
15171517
</YueDisplay>
15181518

1519-
### Try!
1519+
### Try?
15201520

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.
1521+
`try?` is a simplified use for error handling syntax that omit the boolean status from the `try` statement, and it will return the result from the try block when success, return nil instead of error object otherwise.
15221522

15231523
```moonscript
1524-
a, b, c = try! func!
1524+
a, b, c = try? func!
15251525
15261526
-- with nil coalescing operator
1527-
a = (try! func!) ?? "default"
1527+
a = (try? func!) ?? "default"
15281528
15291529
-- as function argument
1530-
f try! func!
1530+
f try? func!
15311531
15321532
-- with catch block
1533-
f try!
1533+
f try?
15341534
print 123
15351535
func!
15361536
catch e
@@ -1539,16 +1539,16 @@ catch e
15391539
```
15401540
<YueDisplay>
15411541
<pre>
1542-
a, b, c = try! func!
1542+
a, b, c = try? func!
15431543

15441544
-- with nil coalescing operator
1545-
a = (try! func!) ?? "default"
1545+
a = (try? func!) ?? "default"
15461546

15471547
-- as function argument
1548-
f try! func!
1548+
f try? func!
15491549

15501550
-- with catch block
1551-
f try!
1551+
f try?
15521552
print 123
15531553
func!
15541554
catch e

doc/docs/zh/doc/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,19 +1516,19 @@ catch err
15161516

15171517
### 错误处理简化
15181518

1519-
`try!``try` 的简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。
1519+
`try?``try` 的功能简化语法,它不再返回 `try` 语句的布尔状态,并在成功时直接返回 `try` 代码块的结果,失败时返回 `nil` 值而非错误对象。
15201520

15211521
```moonscript
1522-
a, b, c = try! func!
1522+
a, b, c = try? func!
15231523
15241524
-- 与空值合并运算符一起使用
1525-
a = (try! func!) ?? "default"
1525+
a = (try? func!) ?? "default"
15261526
15271527
-- 作为函数参数
1528-
f try! func!
1528+
f try? func!
15291529
15301530
-- 带 catch 块的 try!
1531-
f try!
1531+
f try?
15321532
print 123
15331533
func!
15341534
catch e
@@ -1537,16 +1537,16 @@ catch e
15371537
```
15381538
<YueDisplay>
15391539
<pre>
1540-
a, b, c = try! func!
1540+
a, b, c = try? func!
15411541

15421542
-- 与空值合并运算符一起使用
1543-
a = (try! func!) ?? "default"
1543+
a = (try? func!) ?? "default"
15441544

15451545
-- 作为函数参数
1546-
f try! func!
1546+
f try? func!
15471547

15481548
-- 带 catch 块的 try!
1549-
f try!
1549+
f try?
15501550
print 123
15511551
func!
15521552
catch e

spec/inputs/try_catch.yue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ f = ->
7171

7272
do
7373
local func
74-
a, b, c = try! func!
74+
a, b, c = try? func!
7575

7676
do
77-
a, b, c = try! func!
77+
a, b, c = try? func!
7878

7979
do
80-
a = (try! func!) ?? "default"
80+
a = (try? func!) ?? "default"
8181

8282
do
83-
f try! func!
83+
f try? func!
8484

8585
do
86-
f try!
86+
f try?
8787
print 123
8888
func!
8989
catch e
@@ -165,19 +165,19 @@ do
165165

166166
do
167167
local func
168-
a, b, c = try! func!
168+
a, b, c = try? func!
169169

170170
do
171-
a, b, c = try! func!
171+
a, b, c = try? func!
172172

173173
do
174-
a = (try! func!) ?? "default"
174+
a = (try? func!) ?? "default"
175175

176176
do
177-
f try! func!
177+
f try? func!
178178

179179
do
180-
f try!
180+
f try?
181181
print 123
182182
func!
183183
catch e

src/yuescript/yue_ast.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,12 @@ std::string CatchBlock_t::to_string(void* ud) const {
609609
info->popScope();
610610
return line + '\n' + blockStr;
611611
}
612-
std::string Omit_t::to_string(void*) const {
613-
return "!"s;
614-
}
615612
std::string Try_t::to_string(void* ud) const {
616613
auto info = reinterpret_cast<YueFormat*>(ud);
617614
str_list temp;
618615
temp.emplace_back("try"s);
619-
if (omit) {
620-
temp.back() += '!';
616+
if (eop) {
617+
temp.back() += eop->to_string(ud);
621618
}
622619
if (func.is<Exp_t>()) {
623620
temp.back() += (" "s + func->to_string(ud));

src/yuescript/yue_ast.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,11 @@ AST_NODE(CatchBlock)
383383
AST_MEMBER(CatchBlock, &err, &block)
384384
AST_END(CatchBlock)
385385

386-
AST_LEAF(Omit)
387-
AST_END(Omit)
388-
389386
AST_NODE(Try)
390-
ast_ptr<false, Omit_t> omit;
387+
ast_ptr<false, ExistentialOp_t> eop;
391388
ast_sel<true, Block_t, Exp_t> func;
392389
ast_ptr<false, CatchBlock_t> catchBlock;
393-
AST_MEMBER(Try, &omit, &func, &catchBlock)
390+
AST_MEMBER(Try, &eop, &func, &catchBlock)
394391
AST_END(Try)
395392

396393
AST_NODE(Comprehension)

src/yuescript/yue_compiler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = {
7878
"close"s // Lua 5.4
7979
};
8080

81-
const std::string_view version = "0.28.6"sv;
81+
const std::string_view version = "0.28.7"sv;
8282
const std::string_view extension = "yue"sv;
8383

8484
class CompileError : public std::logic_error {
@@ -2332,7 +2332,7 @@ class YueCompilerImpl {
23322332
}
23332333
case id<Try_t>(): {
23342334
auto tryNode = static_cast<Try_t*>(value);
2335-
if (tryNode->omit) {
2335+
if (tryNode->eop) {
23362336
auto assignList = assignment->expList.get();
23372337
std::string preDefine = getPreDefineLine(assignment);
23382338
transformTry(tryNode, out, ExpUsage::Assignment, assignList);
@@ -10055,7 +10055,7 @@ class YueCompilerImpl {
1005510055

1005610056
void transformTry(Try_t* tryNode, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) {
1005710057
auto x = tryNode;
10058-
if (tryNode->omit && usage == ExpUsage::Assignment) {
10058+
if (tryNode->eop && usage == ExpUsage::Assignment) {
1005910059
str_list rets;
1006010060
pushScope();
1006110061
auto okVar = getUnusedName("_ok_"sv);
@@ -10080,7 +10080,7 @@ class YueCompilerImpl {
1008010080
transformAssignment(assignment, out);
1008110081
return;
1008210082
}
10083-
if (tryNode->omit && usage != ExpUsage::Common) {
10083+
if (tryNode->eop && usage != ExpUsage::Common) {
1008410084
auto okVar = getUnusedName("_ok_"sv);
1008510085
auto code = "do\n\t"s + okVar + ", ... = try nil\n\t... if "s + okVar;
1008610086
auto doNode = toAst<Do_t>(code, x);

src/yuescript/yue_parser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,8 @@ YueParser::YueParser() {
501501
return true;
502502
});
503503

504-
Omit = expr('!');
505504
CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block;
506-
Try = key("try") >> -Omit >> space >> (in_block | Exp) >> -CatchBlock;
505+
Try = key("try") >> -ExistentialOp >> space >> (in_block | Exp) >> -CatchBlock;
507506

508507
list_value =
509508
and_(

src/yuescript/yue_parser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ class YueParser {
347347
AST_RULE(ForEach);
348348
AST_RULE(Do);
349349
AST_RULE(CatchBlock);
350-
AST_RULE(Omit);
351350
AST_RULE(Try);
352351
AST_RULE(Comprehension);
353352
AST_RULE(CompValue);

0 commit comments

Comments
 (0)