Skip to content

Give a better error message for stack(array1, array2) #57807

@digital-carver

Description

@digital-carver

It's an easy mistake to make to call stack(array1, array2) instead of stack([array1, array2]) - for eg., having used stack only occasionally, I just tried (something similar to) stack(1:3, 4:6), and got the error message:

ERROR: MethodError: objects of type UnitRange{Int64} are not callable
Use square brackets [] for indexing an Array.
The object of type `UnitRange{Int64}` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Stacktrace:
...

This was really confusing, and had me even checking if I'd accidentally declared a variable named stack somehow. Even looking at the docstring didn't immediately make it obvious why I was getting this particular error, until I realized that it was hitting the

stack(f, args...; [dims])

dispatch, and trying to use 1:3 as the callable f.

It would be a nice improvement to DevEx if this subtle and easy-to-make mistake had a better error message, that more directly hinted at the problem. Something like:

If you're passing multiple iterables to stack, pass them instead as a single iterable of iterables i.e. instead of stack(array1, array2), use stack([array1, array2])

At a glance,

function methods_on_iterable(io, ex, arg_types, kwargs)
(methods_on_iterable in errorshow.jl) seems like a convenient place to add this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]error messagesBetter, more actionable error messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions