-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Add Vector and Matrix constructors #11154
RFC: Add Vector and Matrix constructors #11154
Conversation
I agree that this is missing. could you add a simple test? |
+1, this is something I've wanted for a while. I wonder if we deprecate |
Empty matrices make perfect sense. Even if you can't resize them you can replace them while keeping type stability. It could be argued that it's better to have to be explicit about constructing a 0x0 matrix rather than some other shape of empty matrix, however. |
|
For comparison, OrderedSet(1, 2, 3) but this was deprecated in favor of OrderedSet([1, 2, 3]) |
|
@jiahao, same with |
5a96bd4
to
7bd605a
Compare
OK, I added some tests. I also made Aside from the question of whether |
@@ -15,7 +15,8 @@ typealias StridedVector{T,A<:DenseArray,I<:Tuple{Vararg{RangeIndex}}} Union(Den | |||
typealias StridedMatrix{T,A<:DenseArray,I<:Tuple{Vararg{RangeIndex}}} Union(DenseArray{T,2}, SubArray{T,2,A,I}) | |||
typealias StridedVecOrMat{T} Union(StridedVector{T}, StridedMatrix{T}) | |||
|
|||
call{T}(::Type{Vector{T}}, m::Integer) = Array{T}(m) | |||
call{T}(::Type{Vector{T}}, m::Integer=0) = Array{T}(m) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not call{T}(::Type{Vector{T}}) = Array{T}(0)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, in an earlier revision I added precisely this line instead of doing the current replacement. Why do you prefer it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess your way would be preferable if we were to want to generalize to Matrix
as well, because then we would not want a single-argument constructor to work. Is there any other reason to prefer the method you suggest?
7bd605a
to
d132337
Compare
I went ahead and implemented each constructor for |
+1. The |
RFC: Add Vector and Matrix constructors
I just realized that the constructor is v = Vector{Int}(4)
v[1] = 1
v[2] = 2
v[3] = 3
v[4] = 4
v[5] = 5 # this will grow (Hopefully I'm missing a better way to do it) I think it would be better to have: x = Vector{Int}(1,2,3,4)
y = sizehint!(Vector{Int}(), 500)
z = Matrix{Float64}([1,2,3],[4,5,6],[7,8,9]) Would it help if we had a |
I'm really not convinced that the default empty |
@GlenHertz I think the best existing way to create the vector |
@jiahao I was ultimately swayed by @GunnarFarneback's argument that at times you want to explicitly create an empty matrix for type stability. I've had a need for such a thing in my own code in the past. EDIT: of course, I'm open to further discussion on this. |
I still don't understand the utility of the
thinking it is type stable, but it is not, because of parametric invariance. The return type can only be analyzed statically to yield at best
instead of |
Or Note too that the argument you make could also be made for |
Yes, but my earlier point is that one can want to have |
One of my very earliest stumblings with julia was trying to create empty vectors. The syntax
Int[]
was non-obvious to me, and I kept trying to do things likeVector{Int}()
. #3214 is a step in the right direction, as it allowsVector{T}(0)
. But is there any reason not to allowVector{T}()
? It would be in line withDict()
(orDict{K,V}()
), which creates an empty dictionary. (Not to mention C++'sauto v = std::vector<int>();
, python'sx = list()
, etc., which are in the same style and may form a mental model for people coming from other languages.)On the other hand I don't thinkMatrix{T}()
makes sense, as matrices are not meant to be easily resized in the same way vectors can be, so I have not implemented it.