Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Playing around with Union{}, Type{Union{}}, Type{Core.TypeofBottom}, etc

$
0
0

Consider this weird Julia session:

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.5.3 (2020-11-09)
 _/ |\__'_|_|_|\__'_|  |
|__/                   |

julia> function f1()
               local t = Union{}
               for _ in Base.OneTo(10)
                       println(t)
                       t = Type{t}
               end
               return nothing
       end
f1 (generic function with 1 method)

julia> function f2()
               local t = Union{}
               for _ in Base.OneTo(10)
                       println(t)
                       t = typeof(t)
               end
               return nothing
       end
f2 (generic function with 1 method)

julia> f1()
Union{}
Type{Union{}}
Type{Core.TypeofBottom}
Type{Type{Core.TypeofBottom}}
Type{Type{Type{Core.TypeofBottom}}}
Type{Type{Type{Type{Core.TypeofBottom}}}}
Type{Type{Type{Type{Type{Core.TypeofBottom}}}}}
Type{Type{Type{Type{Type{Type{Core.TypeofBottom}}}}}}
Type{Type{Type{Type{Type{Type{Type{Core.TypeofBottom}}}}}}}
Type{Type{Type{Type{Type{Type{Type{Type{Core.TypeofBottom}}}}}}}}

julia> f2()
Union{}
Core.TypeofBottom
DataType
DataType
DataType
DataType
DataType
DataType
DataType
DataType

I’ve got some questions:

  1. Why does the second iteration of f1() use Type{Union{}}, but subsequent iterations instead use Core.TypeofBottom?

  2. Why does f2() not use Type?

  3. What’s up with DataType, isn’t that for named types? To be specific, the documentation says “DataType represents explicitly declared types that have names, explicitly declared supertypes, and, optionally, parameters.”

  4. Are there any legitimate uses for such constructs (Type{Type{ ... Type{X} ... }}) :smile:

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images