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

Deprecate r + 1 in favor of r .+ 1 #8

Merged
merged 2 commits into from
Feb 1, 2019
Merged

Deprecate r + 1 in favor of r .+ 1 #8

merged 2 commits into from
Feb 1, 2019

Conversation

timholy
Copy link
Member

@timholy timholy commented Feb 1, 2019

This was giving incorrect results for r .+ 1 (it "erased" the axes), because the old correct implementations were for r + 1. I should have noticed this as part of #7, but failed to do so.

So, this is a breaking change. Will release as 0.3.

@timholy
Copy link
Member Author

timholy commented Feb 1, 2019

@mbauman, for Base.IdentityUnitRange we currently have

julia> r = Base.IdentityUnitRange(-2:2)
Base.IdentityUnitRange(-2:2)

julia> r .+ 1
-1:3

julia> axes(r .+ 1)
(Base.OneTo(5),)

Compare with this PR:

julia> r = IdentityRange(-3:3)
IdentityRange(-3:3)

julia> r .+ 1
7-element OffsetArray(::UnitRange{Int64}, -3:3) with eltype Int64 with indices -3:3:
 -2
 -1
  0
  1
  2
  3
  4

There is no type in Base that can represent the correct answer, so should we just have it throw an error?

@mbauman
Copy link
Member

mbauman commented Feb 1, 2019

Oh dang. That's unfortunate. Yup, I agree — we can't keep doing what we were doing.

The problem stems from the use of Abstract[Unit]Range here: https://github.com/JuliaLang/julia/blob/5533f22d8f9a1ba8b6ed5d67eac8d54824489d6a/base/broadcast.jl#L1010

All these abstract range definitions will be wrong for an offset range like Slice and IdentityUnitRange. :-\

@timholy
Copy link
Member Author

timholy commented Feb 1, 2019

Presumably we could make them conditional on having unit axes, so that one can specialize those methods and make them work. Perhaps OffsetArrays could engage in a bit of type-piracy and provide the correct implementations?

@mbauman
Copy link
Member

mbauman commented Feb 1, 2019

That sounds reasonable to me — on both points. It feels a little messy, but I guess we could just dispatch to an internal function that also takes the axes of the given abstract range. That'd allow us to default to an error and allow OffsetArrays to recover the functionality gracefully (well, as gracefully as a peg-legged pirate can be).

@mbauman
Copy link
Member

mbauman commented Feb 1, 2019

Also wrong are .*, ./, and .\. We may want to check other functions for AbstractRange specializations, too — we often just recompute a range whole-cloth based on start/step/stop and totally ignore anything about the axis.

Also fixes r .+ 1, r .* 2, r ./ 2, and 2 .\ r (and other orders)
@timholy
Copy link
Member Author

timholy commented Feb 1, 2019

Thanks for pointing out the errors on .* etc, that still wasn't right here either.

@timholy
Copy link
Member Author

timholy commented Feb 1, 2019

we could just dispatch to an internal function that also takes the axes of the given abstract range. That'd allow us to default to an error and allow OffsetArrays to recover the functionality gracefully

Actually, if we can, let's have the AbstractRange (or other abstract type) implementations just check and then throw an error. Anyone who is going to provide the correct implementation will use the concrete type, so it will get precedence. Maybe a little less ugly that way?

@mbauman
Copy link
Member

mbauman commented Feb 1, 2019

Yeah, that requires OffsetArrays to know all concrete offset range implementations. I suppose there are a very limited number, though, so this probably isn't worth the extra contortions.

Another bug: ==.

@timholy
Copy link
Member Author

timholy commented Feb 1, 2019

Another bug: ==.

Here it was mostly correct because of the first(r) == first(s) == 1, but better would be first(r) == first(s) == first(axes(s)[1]).

@mbauman
Copy link
Member

mbauman commented Feb 1, 2019

Ah, nice. We still need to change it in Base, though... that was my bigger concern.

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

Successfully merging this pull request may close these issues.

2 participants