aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-09-08 09:26:49 +0800
committerLi Jin <dragon-fly@qq.com>2022-09-08 09:26:49 +0800
commitd4af1fa275b1d27229fc995f4a45137380040933 (patch)
tree955c76511d7021e6d1a0f06b46de3852eeac4176 /doc
parentdf85ad2e7f975026ca1e6bd84b26fff81c8d99c8 (diff)
downloadyuescript-d4af1fa275b1d27229fc995f4a45137380040933.tar.gz
yuescript-d4af1fa275b1d27229fc995f4a45137380040933.tar.bz2
yuescript-d4af1fa275b1d27229fc995f4a45137380040933.zip
redesigned metatable syntax. add support for destructuring a field with string and expression
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/docs/doc/README.md53
1 files changed, 27 insertions, 26 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 5f00860..5e81d4c 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -37,8 +37,8 @@ inventory =
37-- metatable manipulation 37-- metatable manipulation
38apple = 38apple =
39 size: 15 39 size: 15
40 index#: {color: 0x00ffff} 40 <index>: {color: 0x00ffff}
41p apple.color, apple.index# if apple.#? 41p apple.color, apple.<index> if apple.<>?
42 42
43-- js-like export syntax 43-- js-like export syntax
44export yuescript = "月之脚本" 44export yuescript = "月之脚本"
@@ -69,8 +69,8 @@ inventory =
69-- metatable manipulation 69-- metatable manipulation
70apple = 70apple =
71 size: 15 71 size: 15
72 index#: {color: 0x00ffff} 72 <index>: {color: 0x00ffff}
73p apple.color, apple.index# if apple.#? 73p apple.color, apple.<index> if apple.<>?
74 74
75-- js-like export syntax 75-- js-like export syntax
76export yuescript = "月之脚本" 76export yuescript = "月之脚本"
@@ -429,77 +429,78 @@ merge = {...a, ...b}
429 429
430### Metatable 430### Metatable
431 431
432The **#** operator can be used as a shortcut for metatable manipulation. 432The **<>** operator can be used as a shortcut for metatable manipulation.
433 433
434* **Metatable Creation** 434* **Metatable Creation**
435Create normal table with key **#** or metamethod key that ends with **#**. 435Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**.
436 436
437```moonscript 437```moonscript
438mt = {} 438mt = {}
439add = (right)=> #: mt, value: @value + right.value 439add = (right)=> <>: mt, value: @value + right.value
440mt.__add = add 440mt.__add = add
441 441
442a = #: mt, value: 1 442a = <>: mt, value: 1
443 -- set field with variable of the same name 443 -- set field with variable of the same name
444b = :add#, value: 2 444b = :<add>, value: 2
445c = add#: mt.__add, value: 3 445c = <add>: mt.__add, value: 3
446 446
447d = a + b + c 447d = a + b + c
448print d.value 448print d.value
449 449
450close _ = close#: -> print "out of scope" 450close _ = <close>: -> print "out of scope"
451``` 451```
452<YueDisplay> 452<YueDisplay>
453<pre> 453<pre>
454mt = {} 454mt = {}
455add = (right)=> #: mt, value: @value + right.value 455add = (right)=> <>: mt, value: @value + right.value
456mt.__add = add 456mt.__add = add
457 457
458a = #: mt, value: 1 458a = <>: mt, value: 1
459 -- set field with variable of the same name 459 -- set field with variable of the same name
460b = :add#, value: 2 460b = :<add>, value: 2
461c = add#: mt.__add, value: 3 461c = <add>: mt.__add, value: 3
462 462
463d = a + b + c 463d = a + b + c
464print d.value 464print d.value
465 465
466close _ = close#: -> print "out of scope" 466close _ = <close>: -> print "out of scope"
467</pre> 467</pre>
468</YueDisplay> 468</YueDisplay>
469 469
470* **Metatable Accessing** 470* **Metatable Accessing**
471Accessing metatable with key **#** or metamethod key that ends with **#**. 471Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**.
472 472
473```moonscript 473```moonscript
474-- create with metatable containing field "value" 474-- create with metatable containing field "value"
475tb = ["value"]#: 123 475tb = <"value">: 123
476tb.index# = tb.# 476tb.<index> = tb.<>
477print tb.value 477print tb.value
478 478
479tb.# = __index: {item: "hello"} 479tb.<> = __index: {item: "hello"}
480print tb.item 480print tb.item
481``` 481```
482<YueDisplay> 482<YueDisplay>
483
483<pre> 484<pre>
484-- create with metatable containing field "value" 485-- create with metatable containing field "value"
485tb = ["value"]#: 123 486tb = <"value">: 123
486tb.index# = tb.# 487tb.<index> = tb.<>
487print tb.value 488print tb.value
488tb.# = __index: {item: "hello"} 489tb.<> = __index: {item: "hello"}
489print tb.item 490print tb.item
490</pre> 491</pre>
491</YueDisplay> 492</YueDisplay>
492 493
493* **Metatable Destructure** 494* **Metatable Destructure**
494Destruct metatable with metamethod key that ends with **#**. 495Destruct metatable with metamethod key surrounded by **<>**.
495 496
496```moonscript 497```moonscript
497{item, :new, :close#, index#: getter} = tb 498{item, :new, :<close>, <index>: getter} = tb
498print item, new, close, getter 499print item, new, close, getter
499``` 500```
500<YueDisplay> 501<YueDisplay>
501<pre> 502<pre>
502{item, :new, :close#, index#: getter} = tb 503{item, :new, :<close>, <index>: getter} = tb
503print item, new, close, getter 504print item, new, close, getter
504</pre> 505</pre>
505</YueDisplay> 506</YueDisplay>