29 Dec 2023, 16:26
It seems to me that pop is expected to be a combination of (1) removing and (2) returning the rightmost element. The removal seems like it happens correctly, whether the element is set or unset. It's the blank return that's odd and undocumented. I think, as I'm remembering now, array item access returning unset (so it can be used with ? and ??, rather than throwing UnsetItemError), might be something already planned? I'm not finding the right thread where it was discussed however.
The following is more of an opinion than a bug report, I guess... After living/using/loving the unset/maybe world for a while now, it seems to me like it might be more consistent if .Default applied wherever an unset Array item's value is accessed. This would include get and __Item, but it would also include __Enum and the return values on popping an unset, as well as single-item Delete/RemoveAt. Am I forgetting others? Currently __Enum actually leaves the value variable unset rather than throwing UnsetItemError, which, at least for me, is the more useful behavior, since using ?? is simpler than catching the error.
It almost seems like UnsetItemError should only occur when one tries to inappropriately assign/pass/use (but not simply access) an unset item (in other words, without the var? notation). This universalizes the opportunity to quickly handle the unset case with ?? without trapping UnsetItemError. In this hypothetical world, (1) if you define .Default, it would never be necessary to ??/catch an unset item, and (2) if you didn't define .Default, then ?? would be available in every place an unset item might be accessed, for easy handling without any inconsistency.
That's obviously just my opinion, and it may be irrelevant, since it would require a potentially breaking change. Maybe if something about array access is in fact in the roadmap, @lexikos might still be considering options? Even as it is, unset is much better than it was before, and the wonderful flexibility of the language makes for easy workarounds for whatever behavior is required. Thanks for all the hard work on this!