StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Should `SArray` constructor create an `SVector` by default?

Open jonniediegelman opened this issue 3 years ago • 2 comments

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

jonniediegelman avatar Oct 06 '22 03:10 jonniediegelman

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

hyrodium avatar Oct 06 '22 07:10 hyrodium

I think SArray creating SVector by default wouldn't be particularly robust. There just needs to be a package that implements ConstructionBase for StaticArrays.

mateuszbaran avatar Oct 06 '22 07:10 mateuszbaran