On Tuesday, March 24, 2015 at 11:01:27 AM UTC+11, Helios De Rosario wrote:
>
> Take this minimal example:
>
> julia> k = ["a", "b"]
> 2-element Array{ASCIIString,1}:
>  "a"
>  "b"
>
> julia> v = [1, 0.5]
> 2-element Array{Float64,1}:
>  1.0
>  0.5
>
>
Notice here that v is now an array of floats, the first element is not an 
integer 1

 

> julia> # This is expected
> julia> Dict(k,v)
> Dict{ASCIIString,Float64} with 2 entries:
>   "b" => 0.5
>   "a" => 1.0
>

So here the value of "a" is a float64 1.0 and inference gets float64 since 
all the values have that type.
 

>
> julia> # But here the values are of type Any, not Float64
> julia> ["a"=>1, "b"=>0.5]
> Dict{ASCIIString,Any} with 2 entries:
>   "b" => 0.5
>   "a" => 1
>
>
Notice here that the value for "a" has remained integer, it has not been 
converted to float64

Since the values have differing types inference decides "any"

Cheers
Lex
 

> It seems that the literal definition of dictionaries infers "Any" when the 
> values (or keys) are not of the same type -- in this example, an integer 
> and a float. In the creation of arrays, for instance, Julia is not that 
> loose (it infers Float64 in this example).
>
> It happens the same with other types and supertypes, e.g. with ASCIIString 
> and UTF8String.
>
> I would have expected the same behaviour in both ways of defining the 
> dictionary. Is there a reason why it is not so?
>
> Regards
> Helios
>

Reply via email to