Stream: helpdesk (published)

Topic: None = Union{}


view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 16:46):

I'm looking at some older code, and I see const None = Union{}. I get the feeling that this originated with some old Julia version. Does anyone know if that was the case?

view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 16:47):

Was this an older version of nothing ?

view this post on Zulip Mason Protter (Apr 19 2021 at 17:28):

Hm, I don't know the history here but I assume not. Union{} can't be instantiated as a value whereas Nothing can. That means that Union{} only exists as a type level construct

view this post on Zulip Mason Protter (Apr 19 2021 at 17:29):

Nowadays, we call Union{} by the name TupeofBottom, meaning it's the 'bottom' of the type hierarchy (where Any is the top)

view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 17:32):

https://github.com/JuliaLang/julia/blob/1b6efc7ab297b7b8aea4d7cb0b52fb589392042b/base/deprecated.jl#L133-L134

view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 17:33):

https://github.com/JuliaLang/julia/blob/dbe775c128a960382e1734a1ad62c09a19e3b592/base/deprecated.jl#L122-L126

view this post on Zulip Mason Protter (Apr 19 2021 at 17:33):

Right, so that shows that None was not an early version of Nothing

view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 18:10):

Perhaps to be concrete, I'm currently looking at https://github.com/jheinen/GR.jl/blob/master/src/GR.jl#L250-L253 . I'm thinking these globals should be converted to const Ref but I would need a fixed type or a perhaps a Union.

view this post on Zulip Mason Protter (Apr 19 2021 at 20:25):

Yeah, that's tricky. Do you know how many possible types this variable can take on?

view this post on Zulip Mark Kittisopikul (Apr 19 2021 at 22:06):

I think most of these could be directly converted to Union{Nothing, String} or Union{Nothing, Int}. In the String case, it looks like we might be able to use empty string. In the Int case, a negative sentinel might work instead.

Perhaps the other question then is how performant is

julia> r = Ref{Union{Nothing,Int}}(5)
Base.RefValue{Union{Nothing, Int64}}(5)

julia> r[] = nothing

julia> r[] = 5
5

view this post on Zulip Jakob Nybo Nissen (Apr 20 2021 at 06:12):

It's extremely efficient - usually optimally efficient. Especially when used with heap-allocated structs

view this post on Zulip Jakob Nybo Nissen (Apr 20 2021 at 06:12):

A Union{Nothing, String} takes up only 8 bytes

view this post on Zulip Jakob Nybo Nissen (Apr 20 2021 at 06:17):

And on master, a few important improvements to the compiler means that unions are handled more efficiently. So the only issue is e.g. Union{Int, Nothing} being 16 bytes.

view this post on Zulip Jakob Nybo Nissen (Apr 20 2021 at 13:37):

Edit: Turns out I was wrong - it's only optimized for bitstypes.


Last updated: Oct 02 2023 at 04:34 UTC