What should the first parameter be when collecting keys from a dictionary?

When using collect(), it requires two parameters, the element type and the collection.

When you’re creating an array from dictionary keys what should the element type parameter be?

character_inventory = Dict{String,Int32}("Sword" => 100, "Staff" => 50)
character_inventory_keys = keys(character_inventory)
 # Prints Base.KeySet{String,Dict{String,Int32}}
println(typeof(character_inventory_keys))
println(collect(character_inventory_keys))

If I place KeySet{String,Dict{String,Int32}} as the first parameter it produces an error.

I’m aware that I don’t necessarily need the type, I can just call collect(character_inventory_keys) but in the interest of learning, I thought I would ask.

The first parameter is the element type of the resulting container, but if you omit it Julia will use the default element type of the iterator:

julia> character_inventory = Dict{String,Int32}("Sword" => 100, "Staff" => 50)
Dict{String,Int32} with 2 entries:
  "Sword" => 100
  "Staff" => 50

julia> collect(character_inventory)
2-element Array{Pair{String,Int32},1}:
 "Sword" => 100
 "Staff" => 50

but you could collect to e.g. Pair{String,Int64} if you want:

julia> collect(Pair{String,Int64}, character_inventory)
2-element Array{Pair{String,Int64},1}:
 "Sword" => 100
 "Staff" => 50
2 Likes