# Is this a bug? \`\`Some(\[\]) != Some(\[\])\`\`

**URL:** https://discourse.julialang.org/t/is-this-a-bug-some-some/39541
**Category:** General Usage
**Tags:** question
**Created:** [May 15, 2020, 5:46pm UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541 "2020-05-15T17:46:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [May 15, 2020, 5:46pm UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/1 "2020-05-15T17:46:23Z")

</div>

I know that there are a couple of open discussions about how to compare general structs by default.

However for the concrete type `Some`,

```julia
Some([]) == Some([])

```

should return `true`, shouldn’t it? But it returns `false`…

Using Julia 1.4.1

* * *

EDIT: adding

```julia
Base.:(==)(a::Some, b::Some) = a.value == b.value

```

fixes the problem, however this should be in `Base`, shouldn’t it?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 16, 2020, 6:49am UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/2 "2020-05-16T06:49:50Z")

</div>

Not all types have `==` defined — this depends on the use case (if they do, `hash` should also be defined for that type; see `?==`).

Since `Some` is almost always used as an intermediate wrapper, I don’t see the strong use case for `==`.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [May 16, 2020, 9:53am UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/3 "2020-05-16T09:53:33Z")

</div>

`Some` is meant to be a transparent wrapper, which indicates that something is definitely not `nothing`.  
as such, I would make it fully transparent, i.e. also `==` should behave the same

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [May 16, 2020, 10:39am UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/4 "2020-05-16T10:39:06Z")

</div>

Since `Some` being a wrapper for objects (not some container type) and both `[]` and `[]` not being the same object, one could argue that this definition of equality of `Some` makes sense.

Maybe `Some` is more like `Ref`? `Ref` has the same behavior. See [Make Refs compare equal when their contents are equal by ararslan · Pull Request #31885 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/31885) and the linked issue for a discussion of the behavior of `==` and `Ref`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 16, 2020, 10:50am UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/5 "2020-05-16T10:50:16Z")

</div>

My understanding is that `Some` is meant for code like

```julia
function f(x, y, z)
    v = g(x, y) # Union{Nothing,Some}
    if v === nothing
        some_default_value(x, z)
    else
        w = something(v)
    end
end

```

where hopefully the whole thing is type stable and `Some` would never leave the function scope, making various optimizations possible. A shortcut mentioned in the docstring `?Some` is

```julia
w = something(v, some_default_value)

```

I don’t think that `Some` wrappers are meant to live on outside the caller, thought that is of course permitted.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [May 16, 2020, 1:07pm UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/6 "2020-05-16T13:07:40Z")

</div>

Having `Some` outside the caller is indeed my case currently

considering the container argument, we indeed have

```julia
[] == [] # true

```

so considering Some as a transparent wrapper, we I expect the same for the wrapped values.

* * *

Cannot tell about Ref, however the type itself suggests something among references, and okay, `[]` and `[]` are two different references. Might be fine to change the definition of equal there…, don’t know.

EDIT: just read into the posted github topic. Indeed the argument seems to be that Ref’s purpose is to mutate the references, i.e. really has reference semantics (mutable struct semantics). However Some is immutable, and really works more like a singleton container.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [May 16, 2020, 1:33pm UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/7 "2020-05-16T13:33:54Z")

</div>

Also there is this long ranging topic [https://github.com/JuliaLang/julia/issues/4648](https://github.com/JuliaLang/julia/issues/4648) about enabling == fallback generically for all immutable structs.

One argument there is that the tendency looks right that immutable structs rather should fallback on using ==, however it is best to be decided for each single type on its own.

Some is immutable, and in all programming languages where I used the equivalent data structure (Haskell, Scala, Java), it behaves like a container.  
Hence I would speak in favour of using ==, i.e. the current behaviour is more like an inconsistency (or overlooked detail).

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [May 16, 2020, 3:45pm UTC](https://discourse.julialang.org/t/is-this-a-bug-some-some/39541/8 "2020-05-16T15:45:42Z")

</div>

Thanks for the small discussion!

I opened this as a change request at Julia github [Adding comparison/hashing to Base.Some · Issue #35911 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/35911)
