Skip to content

confusing types of Complex arrays #10255

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

Closed
catawbasam opened this issue Feb 20, 2015 · 7 comments
Closed

confusing types of Complex arrays #10255

catawbasam opened this issue Feb 20, 2015 · 7 comments

Comments

@catawbasam
Copy link
Contributor

This seems confusing:

julia> ca=[0+0im, 0+0im, 0+0im]
3-element Array{Complex{Int64},1}:
 0+0im
 0+0im
 0+0im

julia> ca2=Complex[0+0im, 0+0im, 0+0im]
3-element Array{Complex{T<:Real},1}:
 0+0im
 0+0im
 0+0im

Commit 4e68cb4* (0 days old master)

@jiahao
Copy link
Member

jiahao commented Feb 20, 2015

This is not a bug, but the result of parametric invariance. ca2 is explicitly defined to have elements of type Complex .

If this issue is about the name Complex being confusing, I had proposed renaming Complex in #8142 , but didn't seem to garner more support.

@catawbasam
Copy link
Contributor Author

But why should ca get typed as Array{Complex{Int64},1} ?
I didn't (intentionally) do anything to narrow the type in the assignment.

@jiahao
Copy link
Member

jiahao commented Feb 20, 2015

Why shouldn't it? How is it different from inferring [1, 2] to be of type Array{Int} or [1, "2"] to be of type Array{Any}?

@JeffBezanson
Copy link
Member

If you don't specify an element type, we pick for you. We prefer a narrow type by default because homogeneous arrays are extremely common and essential for performance.

@catawbasam
Copy link
Contributor Author

Ok, then why not prefer a narrow type for ca2=Complex[0+0im, 0+0im, 0+0im] ?
Aren't those elements also homogenous?

@nalimilan
Copy link
Member

I guess that's because Complex is equivalent to Complex{T<:Real}, so it's equivalent to asking explicitly for an array that can hold any Complex{T<:Real} value. Sometimes you may need this, if you don't plan to store only Complex{Int64} values in the array.

@JeffBezanson
Copy link
Member

Yes, that syntax is for requesting an element type. You're right that we could have decided to have T[...] convert its arguments to type T, followed by doing what [ ... ] does. However then it would be very hard to get a heterogeneous array, and wanting Any[ ... ] is common enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants