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

Request: Vector and Matrix constructors. #10075

Closed
KristofferC opened this issue Feb 4, 2015 · 18 comments
Closed

Request: Vector and Matrix constructors. #10075

KristofferC opened this issue Feb 4, 2015 · 18 comments
Labels
needs decision A decision on this change is needed

Comments

@KristofferC
Copy link
Member

For previous discussion, see: https://groups.google.com/forum/#!topic/julia-users/FBmU-mxQ0k4

My request is to have functionality to be able to initialize a vector of size N and type T using the Vector(T, N) command. This would be equivalent to the current Array(T, N). The reason for this is that it if I have a function taking a Vector argument it feels nice to create the vector I send in to the function using a Vector constructor. Same for matrices.

I tried to implement it myself like this (and in some other ways) but it is not working:

Base.call{T}(::Vector{T}, size::Int) = Array(T, size)
@nalimilan
Copy link
Member

I think what you need is this:

Base.call(::Type{Vector}, T::Type, m::Integer) = Array(T, convert(Int, m))

This is consistent with the definition used for Array (cf. @edit Array(Int, 1)). It ensures that e.g. Vector(Int, BigInt(1)) works.

Note ::Type{Vector}, which means that the argument is the Vector type, not a Vector object.

Could you prepare a PR for this (together with the equivalent code for Matrix, and some tests)? Thanks!

@stevengj
Copy link
Member

stevengj commented Feb 4, 2015

You could also have Base.call{T}(::Type{Vector{T}}, m::Integer) = Array(T, m) to support e.g. Vector{Int}(5). But for that matter shouldn't we also support Array{Int}(5)?

(I don't think the convert(Int, m) is required since that is done in the Array constructor already.)

@StefanKarpinski
Copy link
Member

While we're at it, I think it would be nice if things like Array{Int}(3,4) worked. Arguably that's a better, more consistent way to construct arrays than the current Array(Int,3,4).

@nalimilan
Copy link
Member

@stevengj You're right about convert of course.

Regarding Array{Int}(5), I've often wanted to do that too, but having too many ways of doing the same thing is not great. We should decide which one is the recommended way, and deprecate the other.

@ivarne
Copy link
Member

ivarne commented Feb 4, 2015

Array{Int}(3,4) or Array{Int, 2}(3,4)

@StefanKarpinski
Copy link
Member

I would say that both could be allowed, but Array{Int,2}(3,4) is redundant and the fact that it's a matrix is implied by the dimensions. As for too many ways, I'd favor deprecating Array(Int,3,4).

@milktrader
Copy link
Contributor

Yeah, +1 for deprecating Array(Int,3,4)

This suggests the type is just another parameter, but putting it into curly braces reinforces the notion that types are super-parameters.

@bicycle1885
Copy link
Member

It looks nice and is what I expected to work when I started to play with Julia 👍.
If you are reluctant to do the job, can I take on it?

@KristofferC
Copy link
Member Author

How about things like floor(Int, x)? Should they be written floor{Int}(x) as well?

@bicycle1885
Copy link
Member

That would be a separated question. Array{T,N} is a constructor but floors are not.
If we take the idea, too many functions that take a type as its first argument will be affected (rand(Bool) => rand{Bool}(), convert(Int, 1.0) => convert{Int}(1.0) etc...).

@stevengj stevengj added the needs decision A decision on this change is needed label Feb 5, 2015
@JeffBezanson
Copy link
Member

duplicate of #3214

@JeffBezanson
Copy link
Member

I added these. We can decide whether to do formal deprecations --- prepare yourself for lots of warnings!!

I didn't add Vector(T, n), pending the deprecation decision.

@jakebolewski
Copy link
Member

Is it correct that Array{Int}(1,2) constructs a new array and Array{Int}((1,2)) calls convert(::Type{Array{Int}}, (1,2))?

@jakebolewski
Copy link
Member

I see the old behavior is still retained. So Array{Int}([1,2]) is a conversion and Array{Int}((1,2)) is a construction?

@JeffBezanson
Copy link
Member

Yes.

@jakebolewski
Copy link
Member

If this is going to be the new default behavior, could we deprecate the NTuple constructor?

@JeffBezanson
Copy link
Member

I would be fine with that, but the trouble is that an NTuple carries type information about the number of dimensions, while varargs do not. So we will need to hack around that with an internal definition that still accepts a tuple. There is also something to be said for minimizing splatting, and writing Array{T}(size(x)).

@stevengj
Copy link
Member

See Jeff's commit: 23a6995

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

9 participants