'Difference between String[] and Vector{String}[] in Julia

I have the following code:

x = String[]
y = Vector{String}[]

x == y  # is true

push!(x, "a")  # works fine
push!(y, "a")  # ERROR

The error message is:

ERROR: MethodError: Cannot `convert` an object of type String to an object of type Vector{String}

What is the difference between String[] and Vector{String}[]? Aren't they both vectors of strings?



Solution 1:[1]

T[] where T is a type generates a zero-length vector with elements of type T. So your y is a vector of Vector{String}s. Therefore if you try to push Strings to it you get an error. Try pushing a vector of Strings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S.Surace