Skip to content

map should work on dimension mismatch cases #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
putianyi889 opened this issue Mar 18, 2023 · 4 comments
Open

map should work on dimension mismatch cases #1140

putianyi889 opened this issue Mar 18, 2023 · 4 comments

Comments

@putianyi889
Copy link

julia> map(+,[1,2,3],1:4)
3-element Vector{Int64}:
 2
 4
 6

julia> map(+,SVector(1,2,3),1:4)
ERROR: DimensionMismatch: Sizes (Size(3,), (4,)) of input arrays do not match
Stacktrace:
 [1] _throw_size_mismatch(::SVector{3, Int64}, ::Vararg{Any})
   @ StaticArrays C:\Users\pty\.julia\packages\StaticArrays\4WE4t\src\traits.jl:116
 [2] same_size
   @ C:\Users\pty\.julia\packages\StaticArrays\4WE4t\src\traits.jl:110 [inlined]
 [3] macro expansion
   @ C:\Users\pty\.julia\packages\StaticArrays\4WE4t\src\mapreduce.jl:74 [inlined]
 [4] _map(::typeof(+), ::SVector{3, Int64}, ::UnitRange{Int64})
   @ StaticArrays C:\Users\pty\.julia\packages\StaticArrays\4WE4t\src\mapreduce.jl:40
 [5] map(f::Function, a1::SVector{3, Int64}, as::UnitRange{Int64})
   @ StaticArrays C:\Users\pty\.julia\packages\StaticArrays\4WE4t\src\mapreduce.jl:31
 [6] top-level scope
   @ REPL[14]:1
@hyrodium
Copy link
Collaborator

I did not know that map works on dimension mismatch cases.
I think map(+,SVector(1,2,3),1:2) should throw an error to keep type-stability, so supporting dimension mismatch case would be partial and incomplete.

julia> using StaticArrays

julia> map(+,SVector(1,2,3),1:4) # The output can be SVector(2,4,6)
ERROR: DimensionMismatch: Sizes (Size(3,), (4,)) of input arrays do not match
Stacktrace:
 [1] _throw_size_mismatch(::SVector{3, Int64}, ::Vararg{Any})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/traits.jl:113
 [2] same_size
   @ ~/.julia/dev/StaticArrays/src/traits.jl:107 [inlined]
 [3] macro expansion
   @ ~/.julia/dev/StaticArrays/src/mapreduce.jl:74 [inlined]
 [4] _map(::typeof(+), ::SVector{3, Int64}, ::UnitRange{Int64})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/mapreduce.jl:40
 [5] map(f::Function, a1::SVector{3, Int64}, as::UnitRange{Int64})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/mapreduce.jl:31
 [6] top-level scope
   @ REPL[2]:1

julia> map(+,SVector(1,2,3),1:2) # The output cannot be SVector(2,4) or [2,4] for type-stability
ERROR: DimensionMismatch: Sizes (Size(3,), (2,)) of input arrays do not match
Stacktrace:
 [1] _throw_size_mismatch(::SVector{3, Int64}, ::Vararg{Any})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/traits.jl:113
 [2] same_size
   @ ~/.julia/dev/StaticArrays/src/traits.jl:107 [inlined]
 [3] macro expansion
   @ ~/.julia/dev/StaticArrays/src/mapreduce.jl:74 [inlined]
 [4] _map(::typeof(+), ::SVector{3, Int64}, ::UnitRange{Int64})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/mapreduce.jl:40
 [5] map(f::Function, a1::SVector{3, Int64}, as::UnitRange{Int64})
   @ StaticArrays ~/.julia/dev/StaticArrays/src/mapreduce.jl:31
 [6] top-level scope
   @ REPL[3]:1

@mcabbott
Copy link
Collaborator

I did not know that map works on dimension mismatch cases.

This was added at some point, I think Julia 1.5, perhaps by accident. It also doesn't work for map on tuples: JuliaLang/julia#42216

map(+,SVector(1,2,3),1:2) should throw an error to keep type-stability

Good point.

@matthias314
Copy link

map works for tuples with different lengths by now: JuliaLang/julia#49336

@matthias314
Copy link

In case anybody is interested: The types FixedVector and MutableFixedVector from SmallCollections.jl (analogous to SVector and MVector) accept different lengths:

julia> map(+, FixedVector{3}([1,2,3]), FixedVector{4}(1:4))
3-element FixedVector{3, Int64}:
 2
 4
 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants