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

Array{Int,0}() doesn't work #15907

Closed
nalimilan opened this issue Apr 17, 2016 · 11 comments
Closed

Array{Int,0}() doesn't work #15907

nalimilan opened this issue Apr 17, 2016 · 11 comments
Labels
good first issue Indicates a good issue for first-time contributors to Julia Hacktoberfest Good for Hacktoberfest participants

Comments

@nalimilan
Copy link
Member

nalimilan commented Apr 17, 2016

0-dimensional arrays are inconsistent with other cases. The constructor would probably need to be adapted a bit.

julia> Array{Int,0}() 
ERROR: MethodError: no method matching Array{Int64,0}()
 in eval(::Module, ::Any) at ./boot.jl:236

julia> Array{Int,1}()
0-element Array{Int64,1}

julia> Array{Int,2}()
0x0 Array{Int64,2}

UPDATE: fixed according to comments

@nalimilan nalimilan added the good first issue Indicates a good issue for first-time contributors to Julia label Apr 17, 2016
@yuyichao
Copy link
Contributor

Note that Array{Int,0}(1) should NOT work. Array{Int,0}() should though.

@yuyichao yuyichao changed the title Array{Int,0}(1) doesn't work Array{Int,0}() doesn't work Apr 17, 2016
@eschnett
Copy link
Contributor

What @yuyichao is saying is that array constructors are different from most other collection constructors: They don't take a list of elements to initialize the array; instead, they always produce an uninitialized array. They accept integer arguments to specify the array size, and for a zero-dimensional array, they should accept zero integers.

@nalimilan
Copy link
Member Author

Yes, of course. Though what's slightly annoying is that convert(Array{Int,0}, 1) and convert(Array{Int,1}, 1) don't work either. What's the shortest way to create a 0-dimensional array from a scalar?

@eschnett
Copy link
Contributor

This syntax would be

let x=Array{Int}(); x[]=scalar; x end

If you find a way to avoid the local variable, let me know.

@nalimilan
Copy link
Member Author

Yes, that's the shortest solution I could find. Is there a reason why convert is not supported?

@GunnarFarneback
Copy link
Contributor

setindex!(Array(Int), scalar)

@nalimilan
Copy link
Member Author

@GunnarFarneback Interesting one.

@JeffBezanson
Copy link
Member

I like this game. Here's another one: fill(scalar, ()).

@eschnett
Copy link
Contributor

@yuyichao
Copy link
Contributor

This returns the scalar, not the array.

julia> setindex!(Array(Int), 1)
0-dimensional Array{Int64,0}:
1

The return value of setindex! has nothing to do with the return value of a[x] = b

@cstjean
Copy link
Contributor

cstjean commented Apr 17, 2016

fill(scalar)

@kshyatt kshyatt added the Hacktoberfest Good for Hacktoberfest participants label Oct 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia Hacktoberfest Good for Hacktoberfest participants
Projects
None yet
Development

No branches or pull requests

7 participants