A colon by itself is simply a synonym for `Colon()`, both on 0.3 and 0.4:

julia> :
Colon()

You can use colons in normal function calls and dispatch in both 0.3 and 
0.4:

julia> f(x::Colon) = x
       f(:)
Colon()

That's what's happening with that sub definition.  `sub` acts like 
indexing, but it's just a normal function call.  And so the Colon() passes 
through unchanged in both 0.3 and 0.4.

Indexing expressions are different.  There are some special lowering steps 
to handle colon (just in 0.3) and end (in both 0.3 and 0.4).  "Lowering" is 
an intermediate step between parsing and compilation, and you can see what 
happens to an expression during lowering by calling `expand`.  The expanded 
expressions have some funny things in them (`top(endof)(A)` is a special 
internal pseudo-syntax that is kinda-sorta like saying `Base.endof(A)`), 
but you can see how the `end` keyword lowers to getindex calls in both 0.3 
and 0.4:

julia> expand(:(A[end]))
:(getindex(A,(top(endof))(A)))

julia> expand(:(A[1, end]))
:(getindex(A,1,(top(trailingsize))(A,2)))

julia> expand(:(A[1, end, 3]))
:(getindex(A,1,(top(size))(A,2),3))

So, now to the difference between 0.3 and 0.4: the special lowering step 
for colons in indexing expressions was removed.

# Julia 0.3:
julia> expand(:(A[:]))
:(getindex(A,colon(1,(top(endof))(A))))

# Julia 0.4:
julia> expand(:(A[:]))
:(getindex(A,:))

You can see that in 0.3, it effectively expanded to `1:end`.  But 0.4 just 
left the `:` symbol as it was, and it just gets evaluated normally as an 
argument to getindex.  It's no longer special. So in 0.4, you can 
specialize dispatch on Colon indices.

Make sense?

On Tuesday, September 22, 2015 at 1:07:01 AM UTC-4, Greg Plowman wrote:
>
> Hi All,
> Thanks all for your replies.
>
> OK I can see this will be much easier in v0.4.
> I will revisit when v0.4 released.
>
> I'm still curious about colon and end
>
>> Colon lowering changed in 0.4, 
>
>
> Matt, could you expand on this? How/when is this done in v0.3 vs v0.4?
>
> Does this mean v0.3 code attempting to dispatch on Colon type cannot 
> work? (for example the code from subarray.jl quoted below) 
>
> sub(A::AbstractArray, I::Union(RangeIndex, Colon)...) = sub(A, ntuple(
> length(I), i-> isa(I[i], Colon) ? (1:size(A,i)) : I[i])...)
>
> I noticed that  OffsetArrays (https://github.com/alsam/OffsetArrays.jl 
> <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Falsam%2FOffsetArrays.jl&sa=D&sntz=1&usg=AFQjCNGiyReQIa2MTxw3AB-lqxJiQLAVjA>
>  
> jA 
> <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Falsam%2FOffsetArrays.jl&sa=D&sntz=1&usg=AFQjCNGiyReQIa2MTxw3AB-lqxJiQLAVjA>)
>  
> defines const (..) = Colon(), presumably to use .. in place of :
>
> Will it work in v0.4?
> How and when is end converted?
>
> Thanks again,
> Greg
>
>
>
>

Reply via email to