Skip to content

map with different lengths throws DimensionMismatch, foreach does not #33340

@marekdedic

Description

@marekdedic

The behaviour of using map and foreach on arrays of different lengths is inconsistent:

julia> map((x, y) -> typeof(y), [1, 2, 3], [4, 5])
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at ./indices.jl:154 [inlined]
 [2] _promote_shape at ./iterators.jl:317 [inlined]
 [3] axes at ./iterators.jl:316 [inlined]
 [4] _array_for at ./array.jl:598 [inlined]
 [5] collect(::Base.Generator{Base.Iterators.Zip{Tuple{Array{Int64,1},Array{Int64,1}}},getfield(Base, Symbol("##3#4")){getfield(Main, Symbol("##13#14"))}}) at ./array.jl:611
 [6] map(::Function, ::Array{Int64,1}, ::Array{Int64,1}) at ./abstractarray.jl:2155
 [7] top-level scope at REPL[6]:1

julia> foreach((x, y) -> println(typeof(y)), [1, 2, 3], [4, 5])
Int64
Int64

As far as i can tell, foreach takes the smallest length of an array passed to it and iterates over that, whereas map just throws a DimensionMismatch.

Probably related to #29523, #13361 and #30389

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions