Skip to content

Commit

Permalink
add @compat for .= (closes #285)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Dec 22, 2016
1 parent 90deb1b commit 7ef0dee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Currently, the `@compat` macro supports the following syntaxes:

* `@compat Nullable(value, hasvalue)` to handle the switch from the `Nullable` `:isnull` field to `:hasvalue` field. [#18510](https://github.com/JuliaLang/julia/pull/18510)

* `@compat x .= y` converts to an in-place assignment to `x` (via `broadcast!`) [#17510](https://github.com/JuliaLang/julia/pull/17510).
However, beware that `.=` in Julia 0.4 has the precedence of `==`, not of assignment `=`, so if the right-hand-side `y`
includes expressions with lower precedence than `==` you should enclose it in parentheses `x .= (y)` to ensure the
correct order of evaluation.

## Type Aliases

* `String` has undergone multiple changes: in Julia 0.3 it was an abstract type and then got renamed to `AbstractString` in 0.4; in 0.5, `ASCIIString` and `ByteString` were deprecated, and `UTF8String` was renamed to the (now concrete) type `String`.
Expand Down
6 changes: 6 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ function _compat(ex::Expr)
import Base.writemime
end
end
elseif VERSION < v"0.5.0-dev+5575" && isexpr(ex, :comparison) #17510
if length(ex.args) == 3 && ex.args[2] == :.=
return :(Base.broadcast!(Base.identity, $(_compat(ex.args[1])), $(_compat(ex.args[3]))))
elseif length(ex.args) > 3 && ex.args[2] == :.=
return :(Base.broadcast!(Base.identity, $(_compat(ex.args[1])), $(_compat(Expr(:comparison, ex.args[3:end]...)))))
end
end
return Expr(ex.head, map(_compat, ex.args)...)
end
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,16 @@ let io = IOBuffer()
@test String(take!(io)) == "bbb"
end

# julia#17510
let x = [1,2,3]
@compat x .= [3,4,5]
@test x == [3,4,5]
@compat x .= x .== 4
@test x == [0,1,0]
@compat x .= 7
@test x == [7,7,7]
end

let s = "Koala test: 🐨"
@test transcode(UInt16, s) == UInt16[75,111,97,108,97,32,116,101,115,116,58,32,55357,56360]
@test transcode(UInt32, s) == UInt32[75,111,97,108,97,32,116,101,115,116,58,32,128040]
Expand Down

0 comments on commit 7ef0dee

Please sign in to comment.