diff options
author | Li Jin <dragon-fly@qq.com> | 2022-09-08 09:26:49 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-09-08 09:26:49 +0800 |
commit | d4af1fa275b1d27229fc995f4a45137380040933 (patch) | |
tree | 955c76511d7021e6d1a0f06b46de3852eeac4176 /doc | |
parent | df85ad2e7f975026ca1e6bd84b26fff81c8d99c8 (diff) | |
download | yuescript-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-x | doc/docs/doc/README.md | 53 |
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 |
38 | apple = | 38 | apple = |
39 | size: 15 | 39 | size: 15 |
40 | index#: {color: 0x00ffff} | 40 | <index>: {color: 0x00ffff} |
41 | p apple.color, apple.index# if apple.#? | 41 | p apple.color, apple.<index> if apple.<>? |
42 | 42 | ||
43 | -- js-like export syntax | 43 | -- js-like export syntax |
44 | export yuescript = "月之脚本" | 44 | export yuescript = "月之脚本" |
@@ -69,8 +69,8 @@ inventory = | |||
69 | -- metatable manipulation | 69 | -- metatable manipulation |
70 | apple = | 70 | apple = |
71 | size: 15 | 71 | size: 15 |
72 | index#: {color: 0x00ffff} | 72 | <index>: {color: 0x00ffff} |
73 | p apple.color, apple.index# if apple.#? | 73 | p apple.color, apple.<index> if apple.<>? |
74 | 74 | ||
75 | -- js-like export syntax | 75 | -- js-like export syntax |
76 | export yuescript = "月之脚本" | 76 | export yuescript = "月之脚本" |
@@ -429,77 +429,78 @@ merge = {...a, ...b} | |||
429 | 429 | ||
430 | ### Metatable | 430 | ### Metatable |
431 | 431 | ||
432 | The **#** operator can be used as a shortcut for metatable manipulation. | 432 | The **<>** operator can be used as a shortcut for metatable manipulation. |
433 | 433 | ||
434 | * **Metatable Creation** | 434 | * **Metatable Creation** |
435 | Create normal table with key **#** or metamethod key that ends with **#**. | 435 | Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**. |
436 | 436 | ||
437 | ```moonscript | 437 | ```moonscript |
438 | mt = {} | 438 | mt = {} |
439 | add = (right)=> #: mt, value: @value + right.value | 439 | add = (right)=> <>: mt, value: @value + right.value |
440 | mt.__add = add | 440 | mt.__add = add |
441 | 441 | ||
442 | a = #: mt, value: 1 | 442 | a = <>: mt, value: 1 |
443 | -- set field with variable of the same name | 443 | -- set field with variable of the same name |
444 | b = :add#, value: 2 | 444 | b = :<add>, value: 2 |
445 | c = add#: mt.__add, value: 3 | 445 | c = <add>: mt.__add, value: 3 |
446 | 446 | ||
447 | d = a + b + c | 447 | d = a + b + c |
448 | print d.value | 448 | print d.value |
449 | 449 | ||
450 | close _ = close#: -> print "out of scope" | 450 | close _ = <close>: -> print "out of scope" |
451 | ``` | 451 | ``` |
452 | <YueDisplay> | 452 | <YueDisplay> |
453 | <pre> | 453 | <pre> |
454 | mt = {} | 454 | mt = {} |
455 | add = (right)=> #: mt, value: @value + right.value | 455 | add = (right)=> <>: mt, value: @value + right.value |
456 | mt.__add = add | 456 | mt.__add = add |
457 | 457 | ||
458 | a = #: mt, value: 1 | 458 | a = <>: mt, value: 1 |
459 | -- set field with variable of the same name | 459 | -- set field with variable of the same name |
460 | b = :add#, value: 2 | 460 | b = :<add>, value: 2 |
461 | c = add#: mt.__add, value: 3 | 461 | c = <add>: mt.__add, value: 3 |
462 | 462 | ||
463 | d = a + b + c | 463 | d = a + b + c |
464 | print d.value | 464 | print d.value |
465 | 465 | ||
466 | close _ = close#: -> print "out of scope" | 466 | close _ = <close>: -> print "out of scope" |
467 | </pre> | 467 | </pre> |
468 | </YueDisplay> | 468 | </YueDisplay> |
469 | 469 | ||
470 | * **Metatable Accessing** | 470 | * **Metatable Accessing** |
471 | Accessing metatable with key **#** or metamethod key that ends with **#**. | 471 | Accessing 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" |
475 | tb = ["value"]#: 123 | 475 | tb = <"value">: 123 |
476 | tb.index# = tb.# | 476 | tb.<index> = tb.<> |
477 | print tb.value | 477 | print tb.value |
478 | 478 | ||
479 | tb.# = __index: {item: "hello"} | 479 | tb.<> = __index: {item: "hello"} |
480 | print tb.item | 480 | print 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" |
485 | tb = ["value"]#: 123 | 486 | tb = <"value">: 123 |
486 | tb.index# = tb.# | 487 | tb.<index> = tb.<> |
487 | print tb.value | 488 | print tb.value |
488 | tb.# = __index: {item: "hello"} | 489 | tb.<> = __index: {item: "hello"} |
489 | print tb.item | 490 | print tb.item |
490 | </pre> | 491 | </pre> |
491 | </YueDisplay> | 492 | </YueDisplay> |
492 | 493 | ||
493 | * **Metatable Destructure** | 494 | * **Metatable Destructure** |
494 | Destruct metatable with metamethod key that ends with **#**. | 495 | Destruct 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 |
498 | print item, new, close, getter | 499 | print 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 |
503 | print item, new, close, getter | 504 | print item, new, close, getter |
504 | </pre> | 505 | </pre> |
505 | </YueDisplay> | 506 | </YueDisplay> |