Weird behavior with "map" function

Simple example:

a = [5.4,2.7,8]
b = [5.3,2.1,"Jump"]

typeof(a) #  Vector{Float64}
typeof(b) # Vector{Any}

just as expected. Now try using map function:

map(typeof,[a,b])  

produces this:


2-element Vector{DataType}:
 Vector{Any} (alias for Array{Any, 1})
 Vector{Any} (alias for Array{Any, 1})

What you’re missing is [a,b] promotes.

4 Likes

:+1: :facepunch: Thanks

Although this seemed to work as expected:

a = 5.6  #Float
b = 1  #Integer
c = false  # bool
d = "Jump" # string
e = 1//2 # Rational
[a,b,c,d,e] .|> typeof

I suppose there is no promotion rule for string and bool

What would the promotion of String and Bool be?

3 Likes

It is kind of weird to me that Vector{Float64} is promoted to Vector{Any}. It doesn’t seem like there’d be a performance benefit to allocating a new array with an abstract element type (of course, promotion is easy to get around: Any[a, b]). Besides, it doesn’t look like promoting parameters happen in general:

mutable struct X{T}
    y::T 
end

a = X(1.5)
b = X{Any}(1)
f = [a,b]
#=
2-element Vector{X}:
 X{Float64}(1.5)
 X{Any}(1)
=#