Skip to content

Commit 7ef0dee

Browse files
committed
add @compat for .= (closes #285)
1 parent 90deb1b commit 7ef0dee

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ Currently, the `@compat` macro supports the following syntaxes:
6262

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

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

6772
* `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`.

src/Compat.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,12 @@ function _compat(ex::Expr)
708708
import Base.writemime
709709
end
710710
end
711+
elseif VERSION < v"0.5.0-dev+5575" && isexpr(ex, :comparison) #17510
712+
if length(ex.args) == 3 && ex.args[2] == :.=
713+
return :(Base.broadcast!(Base.identity, $(_compat(ex.args[1])), $(_compat(ex.args[3]))))
714+
elseif length(ex.args) > 3 && ex.args[2] == :.=
715+
return :(Base.broadcast!(Base.identity, $(_compat(ex.args[1])), $(_compat(Expr(:comparison, ex.args[3:end]...)))))
716+
end
711717
end
712718
return Expr(ex.head, map(_compat, ex.args)...)
713719
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,16 @@ let io = IOBuffer()
15251525
@test String(take!(io)) == "bbb"
15261526
end
15271527

1528+
# julia#17510
1529+
let x = [1,2,3]
1530+
@compat x .= [3,4,5]
1531+
@test x == [3,4,5]
1532+
@compat x .= x .== 4
1533+
@test x == [0,1,0]
1534+
@compat x .= 7
1535+
@test x == [7,7,7]
1536+
end
1537+
15281538
let s = "Koala test: 🐨"
15291539
@test transcode(UInt16, s) == UInt16[75,111,97,108,97,32,116,101,115,116,58,32,55357,56360]
15301540
@test transcode(UInt32, s) == UInt32[75,111,97,108,97,32,116,101,115,116,58,32,128040]

0 commit comments

Comments
 (0)