Hi all. I have a function in which I create an array but the type of the array is

Hi all. I have a function in which I create an array but the type of the array is selected by the user meaning that it is passed as an argument something like:

function f(T)
    arr = Matrix{T}(undef,3,3)
    return arr
end

What is a correct type-stable way to do this? This is basically the function to load a bunch of data into named tuples with prespecified (I cannot use DataFrames).

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

Solution from Slack by Valentin:

probably

julia> function f(::Type{T}) where T    
           return Matrix{T}(undef, 3, 3)
       end

you have to remember that the type of Float64 is DataType (like almost all types), so the compiler doesn’t know which type it is. typing ::Type{T} allows the compiler to know which type exactly it is

1 Like