Amon
Amon

Reputation: 432

In Julia, make a Set of Vector, and specifying the type of the Set

In Julia this works as expected:

g1 = [1, 1, 0, 0] / sqrt(2)
u1 = [1, -1, 0, 0] / sqrt(2)
g2 = [0, 0, 1, 1] / sqrt(2)
u2 = [0, 0, 1, -1] / sqrt(2)

up = Set()
push!(up, g1, u1, g2, u2)

gives the result:

Set{Any} with 4 elements:
  [0.0, 0.0, 0.7071067811865475, -0.7071067811865475]
  [0.7071067811865475, 0.7071067811865475, 0.0, 0.0]
  [0.0, 0.0, 0.7071067811865475, 0.7071067811865475]
  [0.7071067811865475, -0.7071067811865475, 0.0, 0.0]

However, the Set is considered to be a Set{Any}, and I would prefer a Set{Array{Float64, 1}} in order to get error should I push something different by mistake.

When I try:

up = Set{Array{Float64, 1}}
push!(up, g1, u1, g2, u2)

I get the following error:

ERROR: MethodError: no method matching push!(::Type{Set{Array{Float64,1}}}, ::Array{Float64,1})
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2158
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2159
  push!(::Array{Any,1}, ::Any) at array.jl:919
  ...
Stacktrace:
 [1] push!(::Type{T} where T, ::Array{Float64,1}, ::Array{Float64,1}) at .\abstractarray.jl:2158
 [2] push!(::Type{T} where T, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at .\abstractarray.jl:2159
 [3] top-level scope at none:0

What would be the correct syntax ?

Upvotes: 2

Views: 827

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

You forgot the constructor it should be up = Set{Array{Float64, 1}}(), see the code below:

julia> up = Set{Array{Float64, 1}}()
Set{Array{Float64,1}} with 0 elements

julia> push!(up, g1, u1, g2, u2)
Set{Array{Float64,1}} with 4 elements:
  [0.0, 0.0, 0.7071067811865475, -0.7071067811865475]
  [0.7071067811865475, 0.7071067811865475, 0.0, 0.0]
  [0.0, 0.0, 0.7071067811865475, 0.7071067811865475]
  [0.7071067811865475, -0.7071067811865475, 0.0, 0.0]

Upvotes: 3

Related Questions