# Nullable and ==

**URL:** <https://discourse.julialang.org/t/nullable-and/4211>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 11, 2017, 6:20pm UTC](https://discourse.julialang.org/t/nullable-and/4211 "2017-06-11T18:20:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![questors](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@questors](https://discourse.julialang.org/u/questors)\
**Post date:** [June 11, 2017, 6:20pm UTC](https://discourse.julialang.org/t/nullable-and/4211/1 "2017-06-11T18:20:42Z")

</div>

Why does Base define:

> ==(x::Nullable, y::Nullable) = throw(NullException())

It means even something as simple as :

> Nullable(0.39) in [Nullable(0.39)]

returns a NullException(). It somewhat limits the utility of the [Iterable Collections](https://docs.julialang.org/en/stable/stdlib/collections/#iterable-collections) abstractions when using Nullables.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 12, 2017, 3:52pm UTC](https://discourse.julialang.org/t/nullable-and/4211/2 "2017-06-12T15:52:01Z")

</div>

That’s because `==` falls back to `===` by default, so `Nullable("a") == Nullable("a")` would return `false`. The choice to return an error was made because we couldn’t decide whether `==` applied to `Nullable` should return a `Bool` or a `Nullable{Bool}` (to follow [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic)).

Maybe we should revisit this. For now, you can use `isequal` instead (of course that doesn’t work for `in`).

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [June 12, 2017, 5:28pm UTC](https://discourse.julialang.org/t/nullable-and/4211/3 "2017-06-12T17:28:16Z")

</div>

Another option might be to use `DataValue` from the [DataValues.jl](https://github.com/davidanthoff/DataValues.jl) package instead of `Nullable`. It has a `==` method that does what you want.

I initially created the package to get around exactly these kinds of problems with `Nullable` in [Query.jl](https://github.com/davidanthoff/Query.jl).

---

<div class="post-metadata">

**Author:** ![questors](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@questors](https://discourse.julialang.org/u/questors)\
**Post date:** [June 13, 2017, 7:40pm UTC](https://discourse.julialang.org/t/nullable-and/4211/4 "2017-06-13T19:40:16Z")

</div>

Gentlemen,

thank you for your responses. I will have a look at [Datavalues.jl](https://github.com/davidanthoff/DataValues.jl).

As I understand it Julia(0.6 RC3) treats a Null as greater than any value, while two Nulls are treated as equal.

But why not set `==` to to `isequal`? At least then `Collections` abstractions would work with the defined logic

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [June 15, 2017, 4:01pm UTC](https://discourse.julialang.org/t/nullable-and/4211/6 "2017-06-15T16:01:53Z")

</div>

`NullableArrays` defines `==`, although it’s essentially type-piracy…

```julia
julia> using NullableArrays
WARNING: Method definition ==(Base.Nullable{S}, Base.Nullable{T}) in module Base at nullable.jl:238 overwritten in module NullableArrays at /Users/cedric/.julia/v0.6/NullableArrays/src/operators.jl:128.

julia> Nullable(1) == Nullable(30)
Nullable{Bool}(false)

```

---

<div class="post-metadata">

**Author:** ![questors](https://avatars.discourse-cdn.com/v4/letter/q/7ba0ec/32.png) [@questors](https://discourse.julialang.org/u/questors)\
**Post date:** [June 15, 2017, 4:33pm UTC](https://discourse.julialang.org/t/nullable-and/4211/7 "2017-06-15T16:33:58Z")

</div>

Thank you for the pointer **cstjean**. You have now explained why I saw a sudden change in behaviour with comparisons a few days ago. I was using `NullableArrays`, but decided to change to `Nullable` for other reasons.
