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

Unexpected type when using comprehensions #4430

Closed
ghost opened this issue Oct 3, 2013 · 5 comments
Closed

Unexpected type when using comprehensions #4430

ghost opened this issue Oct 3, 2013 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 3, 2013

Usually I get a type from comprehensions which I expected, except for the last example below. I find it weird that y becomes an array of Any. I was expecting an array of Float64. I was able to get an array of Float64 by defining x as a const. However that will not be possible in all use cases. How can I avoid this 'weird' behaviour (without using const x or const x2 = copy(x)? Or is this a bug perhaps?

julia> y = [i for i = 1:3]
3-element Array{Int64,1}:
 1
 2
 3

julia> y = [rand() for i = 1:3]
3-element Array{Float64,1}:
 0.263477 
 0.0517146
 0.678627 

julia> x = rand(3)
3-element Array{Float64,1}:
 0.679907
 0.559466
 0.874267

julia> y = [x[i] for i = 1:3]
3-element Array{Any,1}:
 0.679907
 0.559466
 0.874267
@kmsquire
Copy link
Member

kmsquire commented Oct 3, 2013

Inferring types in array comprehensions has frequently been brittle, but
Jeff seems to be improving things when he can. I suggest filing an issue.

In the mean time, assuming the type is always the same, you can get around
it by declaring the type of the array:

julia> x = rand(3)
3-element Array{Float64,1}:
0.205122
0.0720471
0.664416

julia> y = Float64[x[i] for i = 1:3]
3-element Array{Float64,1}:
0.205122
0.0720471
0.664416

Kevin

On Thu, Oct 3, 2013 at 3:23 PM, Carlos Baptista [email protected]:

Usually I get a type from comprehensions which I expected, accept for the
last example below. I find it weird that y becomes an array of Any. I was
expecting an array of Float64. I was able to get an array of Float64 by
defining x as a const. However that will not be possible in all use
cases. How can I avoid this 'weird' behaviour (without using const x or const
x2 = copy(x)? Or is this a bug perhaps?

julia> y = [i for i = 1:3]
3-element Array{Int64,1}:
1
2
3

julia> y = [rand() for i = 1:3]
3-element Array{Float64,1}:
0.263477
0.0517146
0.678627

julia> x = rand(3)
3-element Array{Float64,1}:
0.679907
0.559466
0.874267

julia> y = [x[i] for i = 1:3]
3-element Array{Any,1}:
0.679907
0.559466
0.874267


Reply to this email directly or view it on GitHubhttps://github.com//issues/4430
.

@johnmyleswhite
Copy link
Member

Interesting point: this problem is related to being in the global scope and doesn't happen inside of functions.

julia> function foo()
       x = [1, 2, 3]
       return [x[i]^2 for i in 1:3]
       end
foo (generic function with 1 method)

julia> foo()
3-element Array{Int64,1}:
 1
 4
 9

I think the problem is that, in the global scope, the compiler doesn't feel certain of the type of x.

@kmsquire
Copy link
Member

kmsquire commented Oct 3, 2013

Thanks, John--I've forgotten this point more than once!

On Thu, Oct 3, 2013 at 3:36 PM, John Myles White
[email protected]:

Interesting point: this problem is related to being in the global scope
and doesn't happen inside of functions.

julia> function foo()
x = [1, 2, 3]
return [x[i]^2 for i in 1:3]
end
foo (generic function with 1 method)

julia> foo()
3-element Array{Int64,1}:
1
4
9

I think the problem is that, in the global scope, the compiler doesn't
feel certain of the type of x.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4430#issuecomment-25664147
.

@ghost
Copy link
Author

ghost commented Oct 3, 2013

Thanks Kevin. That did the trick. And likewise I keep on forgetting to check things out in functions. With a background in MATLAB and Python, I still have to get used to that.

@JeffBezanson
Copy link
Member

related: #524, #964

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

3 participants