# Receive the success status of \`push!(Set, element)\`

**URL:** <https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988>\
**Category:** New to Julia\
**Tags:** question, set\
**Created:** [July 24, 2025, 12:30am UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988 "2025-07-24T00:30:05Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 24, 2025, 10:15am UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988/21 "2025-07-24T10:15:31Z")

</div>

It’s worth pointing out that `in!` gives the _opposite_ status of what you want: `in!` is true if the element already existed in the set and no change occurred, while `push_and_get_status!` is true if the element is new and changed the set (well, your initial post’s version always changed the set, but you said that was not ideal so NonDairyNeutrino made it conditional). Therefore, you want the function composition `!in!`.

> [@WalterMadelim](#):
>
> But it might be a pity I cannot spot this `in!` function in the doc.

Search the name on the page and it’s there among a lot of other set operations [Base.in!](https://docs.julialang.org/en/v1/base/collections/#Base.in!). Of course the name would be far easier to discover if there were more links from suggestions in other docstrings, especially from `in, push!, Set`. I evidently didn’t find it on the page either, mostly because I wrongly expected anything like it to also be implemented for dictionaries; in actuality, `in!` was only implemented for sets precisely because of the extra factor of a paired value.

I also checked the implementation, the `Set` method uses internals to avoid redundancy, the fallback `AbstractSet` method has the redundancy and looks pretty much like NonDairyNeutrino’s method but with `true`/`false` swapped.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 24, 2025, 10:42am UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988/22 "2025-07-24T10:42:13Z")

</div>

> [@Benny](#):
>
> your initial post’s version always changed the set

That was because I forget about the `∈` method. I was vague.  
The real intention is explained in my #20 post.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [July 24, 2025, 2:11pm UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988/23 "2025-07-24T14:11:12Z")

</div>

`in!` requires julia 1.11 or greater. This is _not_ a solution for the  
LTS version of julia at 1.10.x

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [July 24, 2025, 4:34pm UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988/24 "2025-07-24T16:34:12Z")

</div>

TIL about `in!`. From the docstring:

```julia
in!(x, s::AbstractSet) -> Bool

If x is in s, return true. If not, push x into s and return false. This is equivalent to in(x, s) ? true : (push!(s, x); false), but may have a more efficient implementation.

```

Such a good idea someone already had it! 🙂

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [July 25, 2025, 8:21am UTC](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988/25 "2025-07-25T08:21:15Z")

</div>

You can use `Base.in!`. It was private in 1.10. Since it’s become public from 1.11 onwards, there is no risk of using it (it’s very very improbable it’ll be deleted in a patch release of 1.10)

[Previous page](https://discourse.julialang.org/t/receive-the-success-status-of-push-set-element/130988.md?page=1)
