From d4af1fa275b1d27229fc995f4a45137380040933 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 8 Sep 2022 09:26:49 +0800 Subject: redesigned metatable syntax. add support for destructuring a field with string and expression --- doc/docs/doc/README.md | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'doc') 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 = -- metatable manipulation apple = size: 15 - index#: {color: 0x00ffff} -p apple.color, apple.index# if apple.#? + : {color: 0x00ffff} +p apple.color, apple. if apple.<>? -- js-like export syntax export yuescript = "月之脚本" @@ -69,8 +69,8 @@ inventory = -- metatable manipulation apple = size: 15 - index#: {color: 0x00ffff} -p apple.color, apple.index# if apple.#? + : {color: 0x00ffff} +p apple.color, apple. if apple.<>? -- js-like export syntax export yuescript = "月之脚本" @@ -429,77 +429,78 @@ merge = {...a, ...b} ### Metatable -The **#** operator can be used as a shortcut for metatable manipulation. +The **<>** operator can be used as a shortcut for metatable manipulation. * **Metatable Creation** -Create normal table with key **#** or metamethod key that ends with **#**. +Create normal table with empty bracekets **<>** or metamethod key which is surrounded by **<>**. ```moonscript mt = {} -add = (right)=> #: mt, value: @value + right.value +add = (right)=> <>: mt, value: @value + right.value mt.__add = add -a = #: mt, value: 1 +a = <>: mt, value: 1 -- set field with variable of the same name -b = :add#, value: 2 -c = add#: mt.__add, value: 3 +b = :, value: 2 +c = : mt.__add, value: 3 d = a + b + c print d.value -close _ = close#: -> print "out of scope" +close _ = : -> print "out of scope" ```
 mt = {}
-add = (right)=> #: mt, value: @value + right.value
+add = (right)=> <>: mt, value: @value + right.value
 mt.__add = add
 
-a = #: mt, value: 1
+a = <>: mt, value: 1
  -- set field with variable of the same name
-b = :add#, value: 2
-c = add#: mt.__add, value: 3
+b = :, value: 2
+c = : mt.__add, value: 3
 
 d = a + b + c
 print d.value
 
-close _ = close#: -> print "out of scope"
+close _ = : -> print "out of scope"
 
* **Metatable Accessing** -Accessing metatable with key **#** or metamethod key that ends with **#**. +Accessing metatable with **<>** or metamethod name surrounded by **<>** or writing some expression in **<>**. ```moonscript -- create with metatable containing field "value" -tb = ["value"]#: 123 -tb.index# = tb.# +tb = <"value">: 123 +tb. = tb.<> print tb.value -tb.# = __index: {item: "hello"} +tb.<> = __index: {item: "hello"} print tb.item ``` +
 -- create with metatable containing field "value"
-tb = ["value"]#: 123
-tb.index# = tb.#
+tb = <"value">: 123
+tb. = tb.<>
 print tb.value
-tb.# = __index: {item: "hello"}
+tb.<> = __index: {item: "hello"}
 print tb.item
 
* **Metatable Destructure** -Destruct metatable with metamethod key that ends with **#**. +Destruct metatable with metamethod key surrounded by **<>**. ```moonscript -{item, :new, :close#, index#: getter} = tb +{item, :new, :, : getter} = tb print item, new, close, getter ```
-{item, :new, :close#, index#: getter} = tb
+{item, :new, :, : getter} = tb
 print item, new, close, getter
 
-- cgit v1.2.3-55-g6feb