-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Closed
Labels
types and dispatchTypes, subtyping and method dispatchTypes, subtyping and method dispatch
Description
I originally encountered this while playing around with the type system in IJulia. Here is a minimum example in that context:
In[1]: T = Tuple{A} where A
Out[1]: Tuple{A} where A
In[2]: print(T.body)
Out[2]: Tuple{A}
In[3]: display(T.body)
TypeError: in new, expected UnionAll, got Type{Tuple{A}}
Stacktrace:
[1] Base.Fix2(::typeof(isequal), ::Type{Tuple{A} where A}) at ./operators.jl:910
[2] isequal(::Type{T} where T) at ./operators.jl:925
[3] undisplay at /Users/student/.julia/packages/IJulia/yLI42/src/display.jl:125 [inlined]
[4] display(::IJulia.InlineDisplay, ::Type{T} where T) at /Users/student/.julia/packages/IJulia/yLI42/src/inline.jl:94
[5] display(::Any) at ./multimedia.jl:323
[6] top-level scope at In[3]:1
I can shorten this to:
In[1]: T = Tuple{A} where A
Out[1]: Tuple{A} where A
In[2]: Base.Fix2(isequal, T)
TypeError: in new, expected UnionAll, got Type{Tuple{A}}
Stacktrace:
[1] Base.Fix2(::typeof(isequal), ::Type{Tuple{A} where A}) at ./operators.jl:910
[2] top-level scope at In[2]:1
At first I could not recreate this in the REPL, but after a lot of playing around I figured out it is dependent on display(T)
and thus Fix2(isequal, T)
being called when showing the result of the first cell.
The following script is a minimal reproducible example in versions 1.2
to 1.4
:
T = Tuple{A} where A
@show Base.Fix2(isequal, T)
@show Base.Fix2(isequal, T.body)
Base.Fix2(isequal, T) = Base.Fix2{typeof(isequal),UnionAll}(isequal, Tuple{A} where A)
ERROR: LoadError: TypeError: in new, expected UnionAll, got Type{Tuple{A}}
Stacktrace:
[1] Base.Fix2(::typeof(isequal), ::Type{Tuple{A} where A}) at ./operators.jl:910
[2] top-level scope at show.jl:613
[3] include(::Module, ::String) at ./Base.jl:377
[4] exec_options(::Base.JLOptions) at ./client.jl:288
[5] _start() at ./client.jl:484
in expression starting at /Users/student/git/jl-julia/ex-minimal.jl:3
This error occurs only in very specific but reproducible circumstances. It does not occur in the following examples:
Fix2
not called on T
first:
T = Tuple{A} where A
@show Base.Fix2(isequal, T.body)
Base.Fix2(isequal, T.body) = Base.Fix2{typeof(isequal),DataType}(isequal, Tuple{A})
Called with different functions (functions other than isequal
do cause an error if used for both):
T = Tuple{A} where A
@show Base.Fix2(isequal, T)
@show Base.Fix2(+, T.body)
Base.Fix2(isequal, T) = Base.Fix2{typeof(isequal),UnionAll}(isequal, Tuple{A} where A)
Base.Fix2(+, T.body) = Base.Fix2{typeof(+),DataType}(+, Tuple{A})
A second UnionAll
identical to the first:
T = Tuple{A} where A
@show Base.Fix2(isequal, T)
try
@show Base.Fix2(isequal, T.body)
catch e
@error e
end
T2 = Tuple{A} where A
@assert T2 === T1
@show Base.Fix2(isequal, T2)
try
@show Base.Fix2(isequal, T2.body)
catch e
@error e
end
Base.Fix2(isequal, T) = Base.Fix2{typeof(isequal),UnionAll}(isequal, Tuple{A} where A)
┌ Error: TypeError(:new, "", UnionAll, Tuple{A})
└ @ Main ~/git/jl-julia/ex-t2.jl:6
Base.Fix2(isequal, T2) = Base.Fix2{typeof(isequal),UnionAll}(isequal, Tuple{A} where A)
Base.Fix2(isequal, T2.body) = Base.Fix2{typeof(isequal),DataType}(isequal, Tuple{A})
Metadata
Metadata
Assignees
Labels
types and dispatchTypes, subtyping and method dispatchTypes, subtyping and method dispatch