Skip to content

Commit a9d28cb

Browse files
committed
Added doc for with? syntax.
1 parent 4f30152 commit a9d28cb

6 files changed

Lines changed: 64 additions & 2 deletions

File tree

doc/docs/doc/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3538,7 +3538,7 @@ with str := "Hello"
35383538
</pre>
35393539
</YueDisplay>
35403540

3541-
Accessing special keys with `[]` in a `with` statement.
3541+
You can access special keys with `[]` in a `with` statement.
35423542

35433543
```moonscript
35443544
with tb
@@ -3561,6 +3561,18 @@ with tb
35613561
</pre>
35623562
</YueDisplay>
35633563

3564+
`with?` is an enhanced version of `with` syntax, which introduces an existential check to safely access objects that may be nil without explicit null checks.
3565+
3566+
```moonscript
3567+
with? obj
3568+
print obj.name
3569+
```
3570+
<YueDisplay>
3571+
<pre>
3572+
with? obj
3573+
print obj.name
3574+
</pre>
3575+
</YueDisplay>
35643576

35653577
## Do
35663578

doc/docs/zh/doc/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ with str := "你好"
34943494
</pre>
34953495
</YueDisplay>
34963496

3497-
在with语句中可以使用`[]`访问特殊键。
3497+
你可以在 `with` 语句中使用 `[]` 访问特殊键。
34983498

34993499
```moonscript
35003500
with tb
@@ -3517,6 +3517,19 @@ with tb
35173517
</pre>
35183518
</YueDisplay>
35193519

3520+
`with?``with` 语法的一个增强版本,引入了存在性检查,用于在不显式判空的情况下安全访问可能为 nil 的对象。
3521+
3522+
```moonscript
3523+
with? obj
3524+
print obj.name
3525+
```
3526+
<YueDisplay>
3527+
<pre>
3528+
with? obj
3529+
print obj.name
3530+
</pre>
3531+
</YueDisplay>
3532+
35203533
## do 语句
35213534

35223535
当用作语句时,do语句的作用就像在Lua中差不多。

spec/inputs/with.yue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ do
165165
break with? tb
166166
break 1
167167

168+
do
169+
a = for i = 1, 100
170+
with? x := tb[i]
171+
break x if .id := 1
172+
168173
nil

spec/outputs/codes_from_doc.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,10 @@ do
21472147
_with_1["key-name"] = value
21482148
end
21492149
_with_0[#_with_0 + 1] = "abc"
2150+
local _with_0 = obj
2151+
if _with_0 ~= nil then
2152+
print(obj.name)
2153+
end
21502154
do
21512155
local var = "hello"
21522156
print(var)
@@ -4363,6 +4367,10 @@ do
43634367
_with_1["key-name"] = value
43644368
end
43654369
_with_0[#_with_0 + 1] = "abc"
4370+
local _with_0 = obj
4371+
if _with_0 ~= nil then
4372+
print(obj.name)
4373+
end
43664374
do
43674375
local var = "hello"
43684376
print(var)

spec/outputs/codes_from_doc_zh.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,10 @@ do
21412141
_with_1["key-name"] = value
21422142
end
21432143
_with_0[#_with_0 + 1] = "abc"
2144+
local _with_0 = obj
2145+
if _with_0 ~= nil then
2146+
print(obj.name)
2147+
end
21442148
do
21452149
local var = "hello"
21462150
print(var)
@@ -4351,6 +4355,10 @@ do
43514355
_with_1["key-name"] = value
43524356
end
43534357
_with_0[#_with_0 + 1] = "abc"
4358+
local _with_0 = obj
4359+
if _with_0 ~= nil then
4360+
print(obj.name)
4361+
end
43544362
do
43554363
local var = "hello"
43564364
print(var)

spec/outputs/with.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,20 @@ do
234234
end
235235
a = _accum_0
236236
end
237+
do
238+
local a
239+
local _accum_0
240+
for i = 1, 100 do
241+
local x = tb[i]
242+
if x ~= nil then
243+
local _des_0 = 1
244+
if _des_0 then
245+
x.id = _des_0
246+
_accum_0 = x
247+
break
248+
end
249+
end
250+
end
251+
a = _accum_0
252+
end
237253
return nil

0 commit comments

Comments
 (0)