On Fri, Oct 16, 2015 at 1:55 PM, Andras Niedermayer
<[email protected]> wrote:
> You can also use `map`, which is better at type inference:

map is not better at type inference. It doesn't use type inference at
all for to determine the return type.

>
> julia> M=10
> 10
>
> julia> [[i] for i=1:M]
> 10-element Array{Any,1}:
>  [1]
>  [2]
>  [3]
>  [4]
>  [5]
>  [6]
>  [7]
>  [8]
>  [9]
>  [10]
>
> julia> map(x->[x],1:M)
> 10-element Array{Array{Int64,1},1}:
>  [1]
>  [2]
>  [3]
>  [4]
>  [5]
>  [6]
>  [7]
>  [8]
>  [9]
>  [10]
>
>
> On Friday, October 16, 2015 at 4:37:26 PM UTC+2, Yichao Yu wrote:
>>
>> On Fri, Oct 16, 2015 at 10:23 AM, Tomas Lycken <[email protected]>
>> wrote:
>> > If you execute this in a function, or in a script file, this won’t
>> > happen.
>> >
>> > The problem when doing it in the REPL is that Julia can’t know what you
>> > will
>> > do with the list variable in the future, so it has to be very defensive
>> > with
>> > type inference, to avoid having to throw nasty type errors later.
>>
>> The issue is related to type inference in global scope but,
>>
>> 1. Putting in a script won't help because it's still in the global scope
>> 2. The issue isn't about future use of list, is't about `M`. Julia
>> have no way to garentee that the type of M is always constant when
>> it's using it (yes, in this case it doesn't change but the compiler
>> cannot know that before running your code)
>> 3. const M = 10 should work
>> 4. putting this in a function should also work
>> 5. There's also discussion about type-inference-free comprehensions.
>>
>> >
>> > // T
>> >
>> > On Friday, October 16, 2015 at 4:19:54 PM UTC+2, Thuener Silva wrote:
>> >>
>> >> I want to create an array of arrays of int64. But sometimes the type
>> >> change to Any, why? It is a bug?
>> >>
>> >> julia> M = 10
>> >> julia> typeof(M)
>> >> Int64
>> >>
>> >> julia> list = [[i] for i=1:M]
>> >> 10-element Array{Any,1}:
>> >>  [1]
>> >>  [2]
>> >>  [3]
>> >>  [4]
>> >>  [5]
>> >>  [6]
>> >>  [7]
>> >>  [8]
>> >>  [9]
>> >>  [10]
>> >>
>> >>  list = [[i] for i=1:10]
>> >> 10-element Array{Array{Int64,1},1}:
>> >>  [1]
>> >>  [2]
>> >>  [3]
>> >>  [4]
>> >>  [5]
>> >>  [6]
>> >>  [7]
>> >>  [8]
>> >>  [9]
>> >>  [10]
>> >>
>> >> Grats,
>> >> Thuener Silva
>> >>
>> >

Reply via email to