Skip to content
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

Broadcasted addition of one CarteseanIndex to an array of them emits cryptic error #41540

Closed
BioTurboNick opened this issue Jul 10, 2021 · 6 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@BioTurboNick
Copy link
Contributor

julia> a = CartesianIndex(1, 2, 3)

julia> b = [a, a, a]
3-element Vector{CartesianIndex{3}}:
 CartesianIndex(1, 2, 3)
 CartesianIndex(1, 2, 3)
 CartesianIndex(1, 2, 3)

julia> b .+ a
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`

# works
julia> b .+ Ref(a)
3-element Vector{CartesianIndex{3}}:
 CartesianIndex(2, 4, 6)
 CartesianIndex(2, 4, 6)
 CartesianIndex(2, 4, 6)

# works
julia> [c + a for c in b]
3-element Vector{CartesianIndex{3}}:
 CartesianIndex(2, 4, 6)
 CartesianIndex(2, 4, 6)
 CartesianIndex(2, 4, 6)

If iteration across the values of CarteseanIndex is not supported, shouldn't it just treat it as an atomic value in the broadcast without the need to wrap it in Ref()?

@JeffBezanson JeffBezanson added the arrays [a, r, r, a, y, s] label Jul 12, 2021
@JeffBezanson
Copy link
Member

American chopper meme of this

@BioTurboNick
Copy link
Contributor Author

BioTurboNick commented Jul 12, 2021

Hah! But seriously, Use `I` rather than `I...`, or use `Tuple(I)... is a bit opaque.

Does something depend on this code path emitting an error?

@JeffBezanson
Copy link
Member

I doubt it is actually necessary; i think it was just to avoid possible confusion about whether a CartesianIndex is container-like.

@BioTurboNick
Copy link
Contributor Author

So would this change be fine then?

broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val,Ptr,AbstractPattern,Pair}) = Ref(x)

# to

broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val,Ptr,AbstractPattern,Pair,AbstractCartesianIndex}) = Ref(x)

Since it doesn't support iteration, this seems to match what broadcastable says should happen.

@JeffBezanson
Copy link
Member

Yeah I'm fine with that; maybe @timholy or @mbauman would want to weigh in.

@MasonProtter
Copy link
Contributor

MasonProtter commented Jan 26, 2023

This would be fixed by #48404 which makes CartesianIndices broadcast scalars (but also iterable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

4 participants