How to get a typed empty array?

I need an empty array with type Tuple{Float64} to solve the following problem.

I want to scatter points using CairoMakie.

The following code works:

using CairoMakie
scatter([(1, 1)])

The following code doesn’t work:

using CairoMakie
a=[]
push!(a, (1, 1))
scatter(a)

I want to use an empty array here because I will push elements using for loop next.

I think I need something like a=[]::Array{Tuple{Float64}} but it doesn’t work.

This should work:

using CairoMakie
a = Array{Tuple{Float64, Float64}}(undef, 1)
for i in 1:5
    push!(a, (i, i))
end
scatter(a)

Note that I have assumed that your data are of type Float64 in general.

2 Likes
julia> A = Tuple{Int64,Int64}[]
Tuple{Int64, Int64}[]

julia> push!(A, (1,1))
1-element Vector{Tuple{Int64, Int64}}:
 (1, 1)

julia> A[1]
(1, 1)

Maybe this is what you need.

5 Likes

If you are going to push! to it, then the size should be zero, not one. Otherwise, there will remain a garbage value at the first index.

5 Likes

Sorry, my bad! You are of course right :slight_smile:

Then

a = Array{Tuple{Float64, Float64}}(undef, 0)

will be what you need.