Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parametrized typealias inconsistently creates subtype relation #8625

Closed
StephenVavasis opened this issue Oct 8, 2014 · 5 comments
Closed
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@StephenVavasis
Copy link
Contributor

If a parametrized type is created by typealias, is it a subtype of the unparametrized type? Here is an example showing that Julia is inconsistent about this question. If I run this in the latest dev version of Julia, the first println yields "false" whereas the second yields "true". By the way, I don't know what the 'correct' answer is to the question.

module testnestparam3

immutable Token{T}
    container::T
end

typealias ArToken{T} Token{Array{T,1}}

println("ArToken{Int} <: ArToken = ", 
            ArToken{Int} <: ArToken)

println("Vector{Float64} <: Vector = ",
        Vector{Float64} <: Vector)

end

kms Edit: formatting

@kmsquire
Copy link
Member

Expanding these out may help understand the question some:

julia> ArToken{Int}
Token{Array{Int64,1}} (constructor with 1 method)

julia> ArToken
Token{Array{T,1}}

julia> Vector{Float64}
Array{Float64,1}

julia> Vector
Array{T,1}

Which means that you're testing

julia> Token{Array{Int64,1}} <: Token{Vector} # === Token{Array{T,1}}
false

julia> Array{Float64,1} <: Vector # === Array{T,1}
true

Julia does not match tokens or type slots beyond the first level, which is probably why these don't work. Indeed, if we only try to match at the first level:

julia> ArToken{Int} <: Token
true

Unfortunately, that doesn't help when you want to specialize on ArToken{T}.

I haven't explored this much, but one way around this might be by using the trick described in #2345 (comment).

Cc: @timholy

@JeffBezanson JeffBezanson added the types and dispatch Types, subtyping and method dispatch label Sep 10, 2015
@timholy
Copy link
Member

timholy commented Sep 19, 2015

I've probably gone through this a half-dozen times, but keep forgetting and having to rediscover the answer. So for the record:

julia> typealias CVec1{T<:Real} Vector{Complex{T}}
Array{Complex{T<:Real},1}

julia> typealias CVec2{C<:Complex} Vector{C}
Array{C<:Complex{T<:Real},1}

julia> typealias CVec3{T<:Real} CVec2{Complex{T}}
Array{Complex{T<:Real},1}

julia> typealias CVec4{T<:Real,C<:Complex} Union{Vector{C},Vector{Complex{T}}}
Union{Array{C<:Complex{T<:Real},1},Array{Complex{T<:Real},1}}

julia> typealias CVec5{T<:Real} CVec4{T,Complex{T}}
Union{Array{Complex{T<:Real},1},Array{Complex{T<:Real},1}}

julia> v = [2.0+3.0im]
1-element Array{Complex{Float64},1}:
 2.0+3.0im

julia> isa(v, CVec1)
false

julia> isa(v, CVec1{Float64})
true

julia> isa(v, CVec2)
true

julia> isa(v, CVec3)
false

julia> isa(v, CVec3{Float64})
true

julia> isa(v, CVec4)
true

julia> isa(v, CVec5)
false

I never seem to stop hoping that CVec5 will work, probably because if I'm being that devious I expect to get away with it 😄.

@StephenVavasis
Copy link
Contributor Author

Tim,

After I made my original posting, I figured out what was going on here, and in fact, I composed some text for the Julia manual:

(begin quote)

However, it is not always the case that a parametric typealias statement creates such a relation; for example, the statement:

typealias AA{T} Array{Array{T,1},1}

does not create the relation AA{Int} <: AA. The reason is that Array{Array{T,1},1} is not an abstract type at all; in fact, it is a concrete type describing a 1-dimensional array in which each entry is an object of type Array{T,1} for some value of T.

(end quote)

But meanwhile, in testing this, I just discovered a bug in 0.4.0-rc1: observe that 'show' for arrays is not working correctly in the following example:

julia> typealias CVec1{T<:Real} Vector{Complex{T}}
Array{Complex{T<:Real},1}

julia> a1 = CVec1()
0-element Array{Complex{T<:Real},1}

julia> f = 6.3
6.3

julia> f2 = Float32(f)
6.3f0

julia> push!(a1,Complex(f,f))
1-element Array{Complex{T<:Real},1}:
 6.3+6.3im

julia> push!(a1,Complex(f2,f2))
2-element Array{Complex{T<:Real},1}:
 6.3+6.3im
 6.3+6.3im

julia> a1[2]
6.3f0 + 6.3f0im

Should I open a bug report?

@timholy
Copy link
Member

timholy commented Sep 20, 2015

That's just showcompact:

julia> show(6.3f0)
6.3f0
julia> showcompact(6.3f0)
6.3

which is fine.

@JeffBezanson
Copy link
Member

closed by #18457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

4 participants