Skip to content

Commit

Permalink
Add checksquare rewrite rule. Fixes #168
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Feb 24, 2016
1 parent aa23cb9 commit 929b515
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ wherever you want to use syntax that differs in the latest Julia

Currently, the `@compat` macro supports the following syntaxes:

* `@compat Base.LinAlg.checksquare` on Julia 0.4

* `@compat Dict(foo => bar, baz => qux)` - type-inferred `Dict` construction. (Also works for `DataStructures.OrderedDict`)

* `@compat Dict{Foo,Bar}(foo => bar, baz => qux)` - type-declared `Dict` construction. (Also works for `DataStructures.OrderedDict`)
Expand Down
20 changes: 20 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ function _compat(ex::Expr)
end
elseif VERSION < v"0.4.0-dev+4389" && f == :withenv
rewrite_pairs_to_tuples!(ex)
elseif VERSION < v"0.5.0-dev+2022"
rewrite_checksquare(ex)
end
elseif ex.head == :curly
f = ex.args[1]
Expand Down Expand Up @@ -440,6 +442,10 @@ function _compat(ex::Expr)
if ex.args[end] == :Timer || ex.args[end] == :(Base.Timer)
ex.args[end] = :(Compat.Timer2)
end
elseif ex.head == :using
if VERSION < v"0.5.0-dev+2022"
rewrite_checksquare(ex)
end
elseif ex.head == :quote && isa(ex.args[1], Symbol)
# Passthrough
return ex
Expand Down Expand Up @@ -859,4 +865,18 @@ else # if VERSION < v"0.5.0-dev+1182"

end

function rewrite_checksquare!(ex::Expr)
a = ex.args
for i in eachindex(a)
ai = a[i]
if isa(ai, Symbol) && ai === :checksquare
a[i] = :chksquare
elseif isa(ai, QuoteNode) && ai.value === :checksquare
a[i] = QuoteNode(:chksquare)
elseif isa(ai, Expr)
rewrite_checksquare!(ai)
end
end
end

end # module
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,9 @@ end
end

@test typeof(displaysize()) == @compat(Tuple{Int, Int})

@test @compat Base.LinAlg.checksquare(randn(4,4)) == 4
@compat begin
using Base.LinAlg.checksquare
@test checksquare(randn(5,5)) == 5
end

0 comments on commit 929b515

Please sign in to comment.