moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Fill operator

Open K4rakara opened this issue 4 years ago • 1 comments

A nice feature to have would be JavaScripts fill operator:

const object1 = { foo: "Hello", bar: "World" } // { foo: "Hello", bar: "World" }
const object2 = { ...object1, bar: "Universe" } // { foo: "Hello", bar: "Universe" }

Now, ... in lua is an operator(?) that copies variable arguments, if they exist.

So, something else would have to be used; heres a few options:

  • ,,, table1:
    • pros: Simple, close to what you would type for ....
    • cons: Looks weird
  • .. table1: Could be used if it is only allowed in situations where there is nothing before the .. operator except for { or ,.
    • pros: Simple, similar to something Lua would do, close to what you would type for ....
    • cons: might clash with the concat operator if not implemented right.

As far as what this could compile to, its fairly simple:

table1 = { foo: "Hello", bar: "World" }
table2 = { ..table1, bar: "Universe" }

Becomes:

table1 = { foo = "Hello", bar = "World" }
table2 = { table.unpack(table1), bar = "Universe" }

K4rakara avatar Feb 13 '21 17:02 K4rakara

i would like this

tommy-mor avatar Nov 07 '21 02:11 tommy-mor