JuliaGeometry / GeometryTypes.jl

Geometry types for Julia
Other
67 stars 41 forks source link

rand(Point2, 10) gives non-concrete type #176

Open JonasIsensee opened 5 years ago

JonasIsensee commented 5 years ago

I noticed the following unfortunate behaviour. Point2 does not specify it's precision but defaults to Float64. However when calling rand(Point2, 2) we receive an array that is not concretely typed.

julia> rand(Point2f0, 2)
2-element Array{Point{2,Float32},1}:
 [0.258088, 0.584784]
 [0.374788, 0.938358]

julia> rand(Point2, 2)
2-element Array{Point{2,T} where T,1}:
 [0.860157, 0.0883986]
 [0.595633, 0.42712]  

julia> rand(Point2)
2-element Point{2,Float64}:
 0.5607439716284568 
 0.29493527430930033
CarpeNecopinum commented 4 years ago

From what I see, this is not really an issue with GeometryTypes.jl, but the underlying StaticArrays.jl that is used for the points.

I tracked this down to

@generated function _rand(rng::AbstractRNG, ::Size{s}, ::Type{SA}) where {s, SA <: StaticArray}
    T = eltype(SA)
    if T == Any
        T = Float64
    end
    v = [:(rand(rng, $T)) for i = 1:prod(s)]
    return quote
        @_inline_meta
        $SA(tuple($(v...)))
    end
end

which is in arraymath.jl of StaticArrays.

Though I don't really think this is even a real defect there, because as result it gives you an Array that you can still push more static arrays with other inner types to, and when you really want just Float64, you can still give it explicitly.