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

multiple type aliases causing "$T (alias for $T)" (type alias that is not a type alias) #39492

Closed
goretkin opened this issue Feb 2, 2021 · 4 comments

Comments

@goretkin
Copy link
Contributor

goretkin commented Feb 2, 2021

julia> VERSION
v"1.6.0-beta1"

julia> struct MyType{A, B} end

julia> const MyTypeA1{B} = MyType{1, B}
MyTypeA1{B} where B (alias for MyType{1, B} where B)

julia> const MyTypeB2{A} = MyType{A, 2}
MyTypeB2{A} where A (alias for MyType{A, 2} where A)

julia> MyType{3,4}
MyType{3, 4}

julia> MyType{1,4}
MyTypeA1{4} (alias for MyType{1, 4})

julia> MyType{3,2}
MyTypeB2{3} (alias for MyType{3, 2})

julia> MyType{1,2}
MyType{1, 2} (alias for MyType{1, 2})

A possible solution is to add an additional condition to check there aren't multiple aliases:

if make_typealias(properx) !== nothing || (unwrap_unionall(x) isa Union && x <: make_typealiases(properx)[2])

At least in this case, it would avoid printing the non-alias type alias.

julia> Base.make_typealiases(MyType{1,2})[1]
2-element Vector{Core.SimpleVector}:
 svec(:(Main.MyTypeA1), svec(2), MyType{1, 2}, (1, -1))
 svec(:(Main.MyTypeB2), svec(1), MyType{1, 2}, (1, -1))

curiosity:
I tried to trigger the analogous issue by defining

const ArrayInt64{N} = Array{Int64, N}

but

julia> Array{Int64, 1}
Vector{Int64} (alias for Array{Int64, 1})

I am not sure why MyType and Array behave differently here:

julia> Base.make_typealias(Array{Int64, 1})
(:(Base.Vector), svec(Int64))

julia> Base.make_typealiases(Array{Int64, 1})
(svec(), Union{})

julia> Base.make_typealias(MyType{1,2})

julia> Base.make_typealiases(MyType{1,2})[1]
2-element Vector{Core.SimpleVector}:
 svec(:(Main.MyTypeA1), svec(2), MyType{1, 2}, (1, -1))
 svec(:(Main.MyTypeB2), svec(1), MyType{1, 2}, (1, -1))

To make the analogy even closer:

julia> const  MyTypeAInt64{B} = MyType{Int64, B}
MyTypeAInt64{B} where B (alias for MyType{Int64, B} where B)

julia> MyType{Int64, 2}
MyType{Int64, 2} (alias for MyType{Int64, 2})

Finally, I think it would be nice to still print the alias, and to choose the "most specific" one:

julia> const MyTypeAInt64B2 = MyType{Int64, 2}
MyType{Int64, 2} (alias for MyType{Int64, 2})

julia> Base.make_typealiases(MyType{Int64,2})[1]
3-element Vector{Core.SimpleVector}:
 svec(:(Main.MyTypeAInt64B2), svec(), MyType{Int64, 2}, (1, 0))
 svec(:(Main.MyTypeAInt64), svec(2), MyType{Int64, 2}, (1, -1))
 svec(:(Main.MyTypeB2), svec(Int64), MyType{Int64, 2}, (1, -1))

in other words, currently

julia> MyTypeAInt64B2
MyType{Int64, 2} (alias for MyType{Int64, 2})

the solution I propose above:

julia> MyTypeAInt64B2
MyType{Int64, 2}

the "most specific" alias:

julia> MyTypeAInt64B2
MyTypeAInt64B2(alias for MyType{Int64, 2})

notes to self:
difference between make_typealiases and make_typealias #36107 (comment)

@goretkin
Copy link
Contributor Author

goretkin commented Feb 2, 2021

I see that this has already been fixed!

julia> VERSION
v"1.7.0-DEV.429"

julia> struct MyType{A, B} end

julia> const MyTypeA1{B} = MyType{1, B}
MyTypeA1{B} where B (alias for MyType{1, B} where B)

julia> const MyTypeB2{A} = MyType{A, 2}
MyTypeB2{A} where A (alias for MyType{A, 2} where A)

julia> MyType{3,4}
MyType{3, 4}

julia> MyType{1,4}
MyTypeA1{4} (alias for MyType{1, 4})

julia> MyType{3,2}
MyTypeB2{3} (alias for MyType{3, 2})

julia> MyType{1,2}
MyType{1, 2}

though the "most specific" alias is not printed:

julia> const MyTypeAInt64B2 = MyType{Int64, 2}
MyType{Int64, 2}

julia> Base.make_typealiases(MyType{Int64,2})[1]
2-element Vector{Core.SimpleVector}:

 svec(:(Main.MyTypeAInt64B2), svec(), MyType{Int64, 2}, (1, 0))
 svec(:(Main.MyTypeB2), svec(Int64), MyType{Int64, 2}, (1, -1))

And I still don't understand why I am perceiving different behavior for Array and MyType.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 2, 2021

Fixed by #39366

@vtjnash vtjnash closed this as completed Feb 2, 2021
@goretkin
Copy link
Contributor Author

goretkin commented Feb 3, 2021

Thanks for the reference and fix. In case you missed it, I proposed yet a different behavior, where the "most specific" alias is printed.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 3, 2021

The code to look at for that is make_typealias (as we only want one). There is no ranking estimate there presently, but PRs would be welcome to adopt one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants