Autovivification documentation is misleading
See original GitHub issueThanks for documenting autovivification in your README. This will provide a response to the many people who run into this, as you’ve no doubt noticed by the number of github tickets related to this (on the react as well as your bugtracker). A few points though:
-
The fundamental reason why autovivifying arrays in JS is difficult is that the same
[]
operator is used for both array and object access so there is no way to disambiguate which one an autovivification is intended to create. However,update
is not implementing JS, but instead its own mini-language. There’s no reason that we can’t decide “path” autovivification is only supported for objects, which in my experience is what is needed in the vast majority of instances (note that all tickets on the react and your bugtrackers are suggesting object autovivification only). -
Even in the cases where you wish to autovivify an array,
$push
,$splice
, and$unshift
can be used to create arrays with no ambiguity (at the leaf level only). Again, anecdotally,$push
seems to be a much more common way of adding to an existing array than$set
. Since it’s obvious$push
and company intend to operate on arrays, there is no problem autovivifying in these cases. -
Your suggested example using
$apply
has three issues from my perspective:$apply
updates cannot be transferred between client and server since functions cannot be serialised- Each
$apply
has two code paths (the default[]
/{}
path and the path for when the target already exists) and therefore ought to be tested in both scenarios - Although subjective, to me this method seems more complicated and difficult to maintain
-
Adding autovification to
update
is not “practically impossible”. In fact, I have implemented it in my version: update-immutable. If you choose to not implement autovivification in your module (a decision I sympathise but do not agree with) then can you at least please link to a “dissenting opinion” on this topic?
Regards,
Doug
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top GitHub Comments
I just run into this issue, at first I couldn’t believe autovivification was not implemented by default. I switched to @hoytech solution, works out of the box. You should really consider implement that solution on your repo, not just in your README, is a must have. Thanks both anyway for making everyone else’s life easier!
Updated docs in
2.3.0