Skip to content

Make it easier to use "inner type parameters" in wrapper types. #38111

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

Closed
jakobnissen opened this issue Oct 20, 2020 · 2 comments
Closed

Make it easier to use "inner type parameters" in wrapper types. #38111

jakobnissen opened this issue Oct 20, 2020 · 2 comments

Comments

@jakobnissen
Copy link
Member

This might be a duplicate of #36377, not sure.

Suppose I want to make a type

struct Foo{I <: Integer}
    v::Vector{Complex{I}}
end

Now, at runtime, I have a vector of integer Complex, but I don't know which integer type, so I can just make an abstractly typed array:

julia> v = Complex{<:Integer}[Complex{Int}(1, 1)]
1-element Array{Complex{var"#s16"} where var"#s16"<:Integer,1}:
 1 + 1im

But now, how do I wrap v in a Foo?

julia> Foo{<:Integer}(v)
ERROR: MethodError: no method matching Foo{var"#s12"} where var"#s12"<:Integer(::Array{Complex{var"#s16"} where var"#s16"<:Integer,1})

One workaround is to instead define

struct Foo{C <: Complex{<:Integer}}
    v::Vector{C}
end

But I think having Complex as part of Foo's signature is unnecessary. Would it be possible to enable the "direct" use of <:Integer in this case?

@JeffBezanson
Copy link
Member

Basically, no --- it's possible to define a constructor for Foo{<:Integer}(v), but that constructor would still have to return an instance of a concrete type, so it would still have to pick a specific value for I.

@jakobnissen
Copy link
Member Author

Ah, I see. So the reason for having to have the wrapper type Complex in Foo{C <: Complex{<:Integer}} is that Foo{<:Integer} is a UnionAll type whereas Foo{Complex{<:Integer}} is concrete.

Thanks for answering this, closing the issue.

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