Consider the following:
julia> k=[-1.0,0.0,2.0]
3-element Array{Float64,1}:
-1.0
0.0
2.0
julia> pow(a,b) = (global k=["foo","bar","baz"]; ^(a,b))
pow (generic function with 1 method)
julia> [pow(k[r],c-1) for r=1:3,c=1:3]
3x3 Array{Any,2}:
1.0 "foo" "foofoo"
"" "bar" "barbar"
"" "baz" "bazbaz"
There are no threads or coroutines – a function invoked by the
comprehension reassigns a global that the comprehension depends on. Of
course, this is an awful thing to do, but it's perfectly possible. Since ^
is just a normal function that happens to be defined when you start up
julia, the same thing could happen when it's called.
On Mon, Mar 23, 2015 at 2:33 PM, Uliano Guerrini <[email protected]>
wrote:
>
>
> Il giorno lunedì 23 marzo 2015 14:02:43 UTC+1, Milan Bouchet-Valat ha
> scritto:
>>
>> That's because type inference has a hard time checking whether the type
>> of k remains the same in the global scope. You can declare k as const,
>> or wrap the code in a function.
>>
>>
> I don't quite get this point:
>
> the only way I see to change k while the comprehension is running would be
> another tread, is this the concern?
>
> Even so: assuming Array{Any,2,2} won't save against a reassignment to k to
> an array of shorter dimension, or a scalar, or a string...
>
> I can't see any gain in assuming Any instead of Float64 but I can see the
> nuisance of having an asymmetric behavior between different scopes.
>
>
>