DanielXMoore / Civet

A TypeScript superset that favors more types and less typing

Home Page:https://civet.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deep `....`rest operator

danielbayley opened this issue · comments

How about something like this?

array := [1, [2, 3], 4]
console.log [....array] # [1, 2, 3, 4]

unpack := (....args) => console.log unpack
unpack(1, [2, 3], 4) # [1, 2, 3, 4]

a := a: { b: 2 }
b := a: { c: 3 }
c := a: 1
console.log { ....a, ....b, ...c } # { a: 1, b: 2, c: 3 }

Perhaps by compiling to something using .flat()

Also thinking maybe .... would flatten one level, while ..... could be fully recursive?

I could see ....array as shorthand for ...array.flat() (and similar magic for function parameters). There are several examples of this in the Civet parser. For example:

  TopLevelSingleLineStatements:first NestedTopLevelStatements*:rest ->
    return [
      ...first.map(s => ["", ...s]),
      ...rest.flat(),
    ]

Is this clearer with ....rest instead? I'm not totally sure. It might be easily misread. ......rest (like two ...s) is maybe more intuitive to me, though we might be getting a little silly notation-wise. What do others think?

I don't think it makes sense to flatten other objects.

Four consecutive dots already has a meaning: https://civet.dev/playground?code=ZiAuLi4u