-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Parametrizing a type for both a container-type field and its element type consistently #19476
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
Comments
|
And #18457 should also make it possible to constraint A with T. |
@yuyichao Could you elaborate on why I don't need |
type MyBetterContainer{A<:AbstractVector}
a::A
MyBetterContainer(v::AbstractVector) = new(v)
end
MyBetterContainer{T<:AbstractVector}(v::T) = MyBetterContainer{T}(v) |
@yuyichao Sorry, probably I edited my previous comment too late and you missed my reference to the Performance Tips documentation. Your example is basically equivalent to
and "Avoid fields with abstract containers" in the Performance Tips documentation exactly starts with it, and gradually evolve it into more sophisticated ones. In doing so, the documentaion provides convincing reason why in some cases we want to introduce |
And the documentation claims that the last example shown there ( If there is no way to close the hole, I think the documentation should mention it, because it gives a false impression that |
You still don't need that type parameter if it's just providing redundant information. You can just replace all use of |
I don't think all usage of
|
|
Thanks for the explanation! So it seems that the documentation went a bit too far in terms of playing with parameters, even though it was an interesting read... |
One of the performance tips entitled "Avoid fields with abstract containers" introduces a nice way to parametrize a type for both a container-type field and its element type. In its conclusion, the tip suggests using type definitions like:
Such definition was suggested as a way to close a "hole" through which users can access the inner constructor with an element type
T
inconsistent with the actual element type of a container typeA
. For example, an attempt to instantiateMyBetterContainer
with a container typeVector{Float64}
and element typeInt64
(rather thanFloat64
) generates an error:However, the hole is not completely closed, because we can still do
Here,
1:5
instead of1.:5
is passed. What happens is1:5
is a subtype ofAbstractVector{Int64}
, it passes the type checking (v::AbstractVector{T}
withT = Int64
) at the boundary of the inner constructor, and1:5
passes the boundary and reaches the constructor body, it is successfully assigned toa::A
withA = Vector{Float64}
because automatic type conversion occurs in field assignment.Here are a couple of questions:
I can imagine that supporting a type definition like this is more complicated, but to me it seems like an ultimate and clean way to prevent inconsistency between
T
and the element type ofA
.The text was updated successfully, but these errors were encountered: