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

Possible source of confusion with Array constructor and parametric type #9370

Closed
IainNZ opened this issue Dec 15, 2014 · 7 comments
Closed
Labels
docs This change adds or pertains to documentation

Comments

@IainNZ
Copy link
Member

IainNZ commented Dec 15, 2014

We were talking in the JuliaOpt office (lol) about type stability, and were musing on the fact that are fairly common error is to do something like

type Foo
  x::Array{Int}
end

which is under-typed, because you need the dimension as well. Why would people not use the dimension? Well, one possible confusion is:

x = Array(Int, 10)  # An array of 10 ints
y::Array{Int, 10}  # y is a dimension 10 array of ints

So the "second argument" has wildly different meanings. Maybe people assume they are same? Or just don't know and punt? Or maybe when people type Array{Int} they think it means Vector{Int}?

Looking for any ideas about whether this is a problem, and if there is any "fix" possible.

(can also move to julia-dev if more appropriate)

@IainNZ IainNZ changed the title Confusion over Array constructor and parametric type Possible source of confusion with Array constructor and parametric type Dec 15, 2014
@ArchRobison
Copy link
Contributor

I think it's a matter of documenting the distinction well. Currently, the brace notation Array{Int,2} is used in the section about arrays, but not explained there. It's explained in the section on type aliases, but that's not where beginning programmers are likely to look.

I suggest adding a highlighted box to the section about arrays. The box would explain the difference between Array(Float32,10) and Array{Float32,10}, and have a hyperlink back to the type aliases section.

@tkelman tkelman added the docs This change adds or pertains to documentation label Dec 15, 2014
@maxruby
Copy link

maxruby commented Dec 15, 2014

I agree that this is confusing. Coming from MATLAB, I found the declaration of Arrays initially very confusing. Especially since the easiest way to start is

array = Int64[] 

It is more verbose and confusing to use

array = Array(Int64, 1) 

Personally, I think the section Typed array initializers should include at least a little more detail about the use of parametric type constructors. The following example in the docs is not immediately obvious to a beginner:

julia> Array[[1 2] [3 4]]
1x2 Array{Array{T,N},2}:  
 1x2 Array{Int64,2}:
 1  2  1x2 Array{Int64,2}:
 3  4

julia> typeof(array)
Array{Array{T,N},2}   <= why is T here not set to Int64?

I would suggest adding a brief reminder of the minimal relevant info from Type declarations, e.g., quoting from that section:

  • Julia’s arrays have type Array{T,N} where T is the element type and N is the number of array dimensions
  • There is no way to equally simply restrict just the dimension but not the element type
  • Type aliases e.g., Vector{T} Array{T,1} are provided to ensure an object is a vector or a matrix (imposing restrictions on the number of dimensions)

@quinnj
Copy link
Member

quinnj commented Dec 15, 2014

I believe there was talk of moving Array construction from

A = Array(Int, 10)

to

A = Array{Int}(10)
#or
A = Vector{Int}(10)

This would match the Dict and Nullable construction syntax.

@maxruby
Copy link

maxruby commented Dec 15, 2014

+1 for more consistency. At least, it would be more clear if all Types followed the same convention when used in declarations.

@jakebolewski
Copy link
Member

How do you handle tuples? Does Array{Int}((1,2,3)) mean Array(Int, (1,2,3)) or convert(::Array{Int}, (1,2,3))?

@JeffBezanson
Copy link
Member

Almost the same as #3214

@KristofferC
Copy link
Member

Array(Int, 10) is now deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

8 participants