Thanks, now I get why it doesn't work as I expected. Enclosing the array
comprehension in a function, as you suggested, works much better
julia> g(y)=[y[1] for i=1]
g (generic function with 1 method)
julia> g(y)
1-element Array{Float64,1}:
1.0
W dniu czwartek, 17 kwietnia 2014 17:03:53 UTC+2 użytkownik Tim Holy
napisał:
>
> A more elaborate example:
>
> julia> y = [1,2]
> 2-element Array{Int64,1}:
> 1
> 2
>
> julia> [(y = float64(y); y[i]) for i = 1:2]
> 2-element Array{Any,1}:
> 1.0
> 2.0
>
> When your code is inside a function, julia will analyze it and determine
> that
> the type isn't changing.
>
> --Tim
>
> On Thursday, April 17, 2014 07:56:28 AM John Myles White wrote:
> > y = [1]
> >
> > y = [1.0]
> >
> > [y[1] for i = 1]
> >
> > On Apr 17, 2014, at 7:44 AM, Paweł Biernat
> > <[email protected]<javascript:>>
> wrote:
> > > Can you elaborate on that? How can you change the type of y in my
> > > example?
> > >
> > > W dniu czwartek, 17 kwietnia 2014 16:38:48 UTC+2 użytkownik John Myles
> > > White napisał: Because list comprehensions in the global scope can't
> be
> > > sure that you aren't changing the type of y along the way.>
> > > -- John
> > >
> > > On Apr 17, 2014, at 7:37 AM, Paweł Biernat <[email protected]>
> wrote:
> > >> Could someone please explain to me the difference between the two
> cases?
> > >>
> > >> julia> y=[1]
> > >>
> > >> 1-element Array{Int64,1}:
> > >> 1
> > >>
> > >> julia> [y[1] for i=1]
> > >>
> > >> 1-element Array{Any,1}:
> > >> 1
> > >>
> > >> julia> [y[1]]
> > >>
> > >> 1-element Array{Int64,1}:
> > >> 1
> > >>
> > >> In particular, why is in the first case the element type is Any, but
> in
> > >> the second case it is Int64?
>