-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the two meanings of delete! #3405
Comments
It is not possible for a function to return |
That's why I put it in brackets and said it was possibly "awful/infeasible". |
|
Yeah, the name is clearly up for debate. Any other ideas for a name? |
|
I do rather like the idea that |
The idea of |
Fine with me. Python has |
This kind of implies that we should also have |
It's slightly unfortunate that none of these provide a way to delete a range of indices in an array without allocating an array of the removed values. |
Perhaps |
This has bothered me too. Thankfully |
This is really the same distinction: you want one version that just deletes stuff and throws it away and one version that returns the removed items too. |
This is a possible gotcha if |
Yikes. That's a slightly terrifying prospect. Array resizing and views do not seem like a good mix at all. |
The only guarantee I currently provide on combinations of resizing and reshaping is that they won't segfault :) |
RFC: Split delete!(h::Dict,key) into pop!(), delete!() (#3405)
I was looking at the
delete!
function and thinking how requiring that the value to be deleted exist in the collection is often annoying. More annoying still is that it makes what could be an idempotent function non-idempotent. The underlying issue is that there are two ways of usingdelete!
:Returning a value from
delete!
is primarily useful for associative collections, where the value associated with the deleted key is returned. This is often handy. This is also whydelete!(a,k)
throws an error ifa
doesn't contain the keyk
since the method doesn't have any value to return. If the three-argument form,delete!(a,k,d)
, is used, there there is a default value to return, so no error is/need be thrown.When this logic is applied to non-associative collections, it becomes largely nonsensical: since you already know the value you're deleting, it's unusual to also care about what
delete!
returns. The exception is when you have mutable objects that hash the same, so object used to do the lookup and the object that's returned are actually distinguishable. We're not even handling this situation correctly at the moment since I think there are cases wheredelete!(s,x)
returnsx
rather than object equal tox
that's ins
.All this leads me to wonder if we shouldn't separate the two meanings of
delete!
into:delete!(c,x)
which returns nothing and is idempotent,take!(c,x)
which returns the associated value and throws an error ifc
doesn't containx
and thus is not idempotent.Although it would be infrequently used,
take!
could be implemented for non-associative collections and should return the value stored in the collection, not the one passed into the call, so that it is useful in the cases where those objects are mutable and therefore distinguishable. A further distinction could be thatdelete!
returns the collection, making it friendly to chaining operations, whereastake!
obviously would not return the collection since returning a value is its raison d'etre.[Another interesting but possibly awful/infeasible idea is that
delete!
could returnundef
when the deleted value doesn't exist. Then if someone uses the returned value in any way an error is thrown upon usage. Thus, if you're usingdelete!
in the idempotent way, ignoring its return value, there's no problem with removing a non-existent key. But if you use the return value ofdelete!
and the key didn't exist, it would be an error. This is probably too tricky and magical though.]The text was updated successfully, but these errors were encountered: