Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

StaticArrays field values printed as undef?

$
0
0

I’m using a StaticArrays.jl FieldVector for a 3-vector that can be both accessed by field name (v.x) as well as by index (v[1]). I also added a length function to get the vector length:

using StaticArrays
using LinearAlgebra

struct vec3 <: FieldVector{3, Float32}
    x::Float32
    y::Float32
    z::Float32
    
    vec3() = zeros(Float32, 3)
    vec3(x, y, z) = new(x, y, z)
end

import Base.length
length(v::vec3) = sqrt(dot(v,v))

v = vec3(0.5f0, 0.0f0, 0.5f0)

println(v)
println(v[1], v.x)
println(length(v))

The import Base.length seems to screw up the printing of v, as I get the following output:

Float32[#undef, #undef, #undef]
0.50.5
0.70710677

The first line suggests the vector fields have an undefined value, but the other two lines show the values are actually correct, they’re merely printed incorrectly. Removing the import Base.length line fixes this and produces:

Float32[0.5, 0.0, 0.5]
0.50.5
0.70710677

Is this an expected side-effect of the import, or a bug?

8 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles