whyrusleeping / gx

A package management tool
MIT License
1.88k stars 110 forks source link

Cannot add to an array with gx set #137

Open vklquevs opened 7 years ago

vklquevs commented 7 years ago

It's possible to create an array and edit existing entries, but not add or delete.

whyrusleeping commented 7 years ago

@vklquevs Do you have any syntax in mind for how this should work?

vklquevs commented 7 years ago

I thought about that for a while before creating this issue, and could only come up with 3 things to add an element to the end of an array:

  1. PHP-like gx set .foo[] bar
  2. New command gx append .foo bar
  3. Specify a non-existent element and it creates it, i.e. gx set .foo[0] bar

My thoughts:

  1. Simple, but slightly awkward special case in syntax, with different behaviour to gx view .foo[]
  2. Simplest, but it seems like overkill to have a new command just to add to an array, when we already have gx set to add to an object
  3. Intuitive, but requires an array.length getter just to reliably add a new item, e.g. gx set .foo[.foo.length] bar (could be verbose depending on array length syntax)

Option 3 would have to deal with the user attempting to introduce gaps in the array (probably just with an error message). It would also have the side-effect of introducing an array length getter by necessity of ergonomics (previously I have looped over an array by incrementing the index until gx crashed, but that's a different story).

To me, these options are fairly equally match in pros and cons, with each having problems the others don't.