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

add @compat for .= #292

Merged
merged 1 commit into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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