StaticArrays.jl
StaticArrays.jl copied to clipboard
Should `SArray` constructor create an `SVector` by default?
julia> args = (0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
julia> SArray(args...)
ERROR: DimensionMismatch: No precise constructor for SArray found. Length of input was 3.
Stacktrace:
[1] _no_precise_size(SA::Type, x::Tuple{Float64, Float64, Float64})
@ StaticArrays ~/.julia/packages/StaticArrays/PUoe1/src/convert.jl:156
[2] _no_precise_size(SA::Type, x::StaticArrays.Args{Tuple{Float64, Float64, Float64}})
@ StaticArrays ~/.julia/packages/StaticArrays/PUoe1/src/convert.jl:155
[3] adapt_size(#unused#::Type{SArray}, x::StaticArrays.Args{Tuple{Float64, Float64, Float64}})
@ StaticArrays ~/.julia/packages/StaticArrays/PUoe1/src/convert.jl:112
[4] construct_type(#unused#::Type{SArray}, x::StaticArrays.Args{Tuple{Float64, Float64, Float64}})
@ StaticArrays ~/.julia/packages/StaticArrays/PUoe1/src/convert.jl:87
[5] SArray(::Float64, ::Vararg{Float64})
@ StaticArrays ~/.julia/packages/StaticArrays/PUoe1/src/convert.jl:160
[6] top-level scope
@ REPL[6]:1
This makes it hard to robustly recreate things from their types. ConstructionBase.jl, for example, uses nameof the type to get a constructor that will be robust to things like reconstructing with different eltypes.
julia> using ConstructionBase
julia> T = constructorof(typeof(SA[0.0, 0.0, 0.0]))
SArray
julia T(0, 0, 0)
# Same error
Versions I'm using:
(jl_VtnNO2) pkg> st
Status `/private/var/folders/4z/x1xvpm1d3jd0x24qgrlk9ksm0000gp/T/jl_VtnNO2/Project.toml`
[187b0558] ConstructionBase v1.4.1
[90137ffa] StaticArrays v1.5.9
julia> versioninfo()
Julia Version 1.8.1
Commit afb6c60d69a (2022-09-06 15:09 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin21.4.0)
CPU: 16 × Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
Threads: 1 on 8 virtual cores
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS =
JULIA_PKG_DEVDIR = /Users/jonnie.diegelman/dev
This issue will be fixed with #1086, but I'm not sure we will have an additional dependency in this package.
julia> using ConstructionBase
julia> T = constructorof(typeof(SA[0.0, 0.0, 0.0]))
SVector (alias for SArray{Tuple{S}, T, 1, S} where {S, T})
julia> T(0,0,0)
3-element SVector{3, Int64} with indices SOneTo(3):
0
0
0
I think SArray creating SVector by default wouldn't be particularly robust. There just needs to be a package that implements ConstructionBase for StaticArrays.