aitzkora
aitzkora

Reputation: 328

What is the logic for typing an element of an array in Julia

I want to use an array of Set of integers in Julia, but when I look a the type of each element, it is not a Set, Why ?

typeof(fill(Set{Int64}[],3)[2])

returns

 Array{Set{Int64},1}

and not

 Set{Int64}

If I used primitive types, for example,

 typeof([1,2][1]) # returns Int64

but

 typeof([Set{Int64}[],Set{Int64}[]][1]) # returns Array{Set{Int64},1}

Why there is a Array enclosing Set{Int64}

Upvotes: 4

Views: 70

Answers (1)

Frames Catherine White
Frames Catherine White

Reputation: 28242

You wrote typeof(fill(Set{Int64}[],3)[2])

Set{Int}[] is another way of writing Vector{Set{Int}}. Generally this way makes more sense when usd on a nonempty array construction like Int128[1,2,3]

You probably wanted Set{Int}() to get a single set. Check typeof(fill(Set{Int64}(), 3)[2])

However this will fill the array with 3 references to the same set. So mutating one will mutate them all. Rarely what you want.

Probably what you really wanted was: [Set{Int}() for _ in 1:3]

Upvotes: 5

Related Questions