fix(package): [slice] functions with inconsistent return behaviour #326
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Duke and all Lancet enthusiasts,
The following functions in the
slice
package have inconsistent behaviour inits return value:
DeleteAt
Drop
DropRight
InsertAt
UpdateAt
Without
These functions return the same slice passed as input (in place modification) on
some conditions, and a copy of the input slice (no in place modification) on other
conditions. I use the
DeleteAt
function as an example:Note that in case the slice is empty or the deletion index is negative, the return value
is the same passed slice. In this case, there is no modification of the passed input.
But if the code proceeds further, it will return a copy of the passed input.
This return/modification behaviour can be a source of confusion, as sometimes the user
will receive a copy, and sometimes the same passed slice.
This fix goes over all the mentioned functions above and corrects them for this
inconsistent behaviour. The correction is applied in such form that a copy of the
passed slice is returned -- never a modified version of the passed slice.
Other remarks:
or it returns a modified copy for all cases. There is no fix. I consider this is
the desired functionality of the function.
Unique
,UniqueBy
,UniqueByComparator
,UniqueByField
makesmodifications in place in all situations. There is no inconsistency in this
case. However, if the intention is that these functions also return copies, than
they still need to be reviewed.
SymmetricDifference
uses Unique on the input slice. That makes itindirectly inconsistent. This fixes addressed that.