Skip to content

Commit

Permalink
don't call at-compat from at-views, add at-dotcompat to combine at-do…
Browse files Browse the repository at this point in the history
…t and at-compat
  • Loading branch information
stevengj committed Feb 5, 2017
1 parent 1b638f5 commit 72eaa64
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ Currently, the `@compat` macro supports the following syntaxes:
## New functions, macros, and methods

* `@views` takes an expression and converts all slices to views ([#20164]), while
`@view` ([#16564]) converts a single array reference to a view ([#20164]). Using `@views`
automatically implies `@compat`.
`@view` ([#16564]) converts a single array reference to a view ([#20164]).

* `@__dot__` takes an expression and converts all assignments, function calls,
and operators to their broadcasting "dot-call" equivalents ([#20321]). In Julia 0.6, this
can be abbreviated `@.`, but that macro name does not parse in earlier Julia versions.
Using `@__dot__` automatically implies `@compat`.
For this to work in older versions of Julia (prior to 0.5) that don't have dot calls,
you should instead use `@dotcompat`, which combines the `@__dot__` and `@compat` macros.

* `foreach`, similar to `map` but when the return value is not needed ([#13744])

Expand Down
13 changes: 11 additions & 2 deletions src/arraymacros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ if !isdefined(Base, Symbol("@views"))
end

macro views(x)
esc(_compat(_views(replace_ref_end!(x))))
esc(_views(replace_ref_end!(x)))
end
export @views
end
Expand Down Expand Up @@ -214,7 +214,16 @@ if !isdefined(Base, Symbol("@__dot__"))
end
end
macro __dot__(x)
esc(__dot__(x))
end
macro dotcompat(x)
esc(_compat(__dot__(x)))
end
export @__dot__
export @__dot__, @dotcompat
else
# in 0.6, use the __dot__ function from Base.Broadcast
macro dotcompat(x)
esc(_compat(Base.Broadcast.__dot__(x)))
end
export @dotcompat
end
47 changes: 37 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,7 @@ end
let X = reshape(1:24,2,3,4), Y = 4:-1:1
@test isa(@view(X[1:3]), SubArray)

@test X[1:end] == @__dot__ (@view X[1:end]) # test compatibility of @. and @view
@test X[1:end] == @dotcompat (@view X[1:end]) # test compatibility of @. and @view
@test X[1:end-3] == @view X[1:end-3]
@test X[1:end,2,2] == @view X[1:end,2,2]
@test reshape(X[1,2,1:end-2],2) == @view X[1,2,1:end-2]
Expand All @@ -1647,7 +1647,7 @@ let X = reshape(1:24,2,3,4), Y = 4:-1:1
end

# test @views macro
@views let f!(x) = x[1:end-1] .+= x[2:end].^2
@views @compat let f!(x) = x[1:end-1] .+= x[2:end].^2
x = [1,2,3,4]
f!(x)
@test x == [5,11,19,4]
Expand All @@ -1662,15 +1662,42 @@ let X = reshape(1:24,2,3,4), Y = 4:-1:1
@test x == [5,6,35,4]
x[Y[2:3]] .= 7:8
@test x == [5,8,7,4]
@__dot__ x[(3,)..., ()...] += 3 # @. should convert to .+=, test compatibility with @views
@dotcompat x[(3,)..., ()...] += 3 # @. should convert to .+=, test compatibility with @views
@test x == [5,8,10,4]
i = Int[]
# test that lhs expressions in update operations are evaluated only once:
x[push!(i,4)[1]] += 5
@test x == [5,8,10,9] && i == [4]
x[push!(i,3)[end]] += 2
@test x == [5,8,12,9] && i == [4,3]
@__dot__ x[3:end] = 0 # make sure @. works with end expressions in @views
@dotcompat x[3:end] = 0 # make sure @. works with end expressions in @views
@test x == [5,8,0,0]
end
# same tests, but make sure we can switch the order of @compat and @views
@compat @views let f!(x) = x[1:end-1] .+= x[2:end].^2
x = [1,2,3,4]
f!(x)
@test x == [5,11,19,4]
@test isa(x[1:3],SubArray)
@test x[2] === 11
@test Dict((1:3) => 4)[1:3] === 4
x[1:2] = 0
@test x == [0,0,19,4]
x[1:2] .= 5:6
@test x == [5,6,19,4]
f!(x[3:end])
@test x == [5,6,35,4]
x[Y[2:3]] .= 7:8
@test x == [5,8,7,4]
@dotcompat x[(3,)..., ()...] += 3 # @. should convert to .+=, test compatibility with @views
@test x == [5,8,10,4]
i = Int[]
# test that lhs expressions in update operations are evaluated only once:
x[push!(i,4)[1]] += 5
@test x == [5,8,10,9] && i == [4]
x[push!(i,3)[end]] += 2
@test x == [5,8,12,9] && i == [4,3]
@dotcompat x[3:end] = 0 # make sure @. works with end expressions in @views
@test x == [5,8,0,0]
end
@views @test isa(X[1:3], SubArray)
Expand All @@ -1691,19 +1718,19 @@ end

# @. (@__dot__) tests, from base:
let x = [4, -9, 1, -16]
@test [2, 3, 4, 5] == @__dot__(1 + sqrt($sort(abs(x))))
@test @__dot__(x^2) == x.^2
@__dot__ x = 2
@test [2, 3, 4, 5] == @dotcompat(1 + sqrt($sort(abs(x))))
@test @dotcompat(x^2) == x.^2
@dotcompat x = 2
@test x == [2,2,2,2]
end
@test [1,4,9] == @__dot__ let x = [1,2,3]; x^2; end
@test [1,4,9] == @dotcompat let x = [1,2,3]; x^2; end
let x = [1,2,3], y = x
@__dot__ for i = 1:3
@dotcompat for i = 1:3
y = y^2 # should convert to y .= y.^2
end
@test x == [1,256,6561]
end
let x = [1,2,3]
@__dot__ f(x) = x^2
@dotcompat f(x) = x^2
@test f(x) == [1,4,9]
end

0 comments on commit 72eaa64

Please sign in to comment.