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

Compat for Julia #18510, #18484 #287

Merged
merged 2 commits into from
Oct 18, 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `@compat @boundscheck checkbounds(...)` rewrites to unconditionally call `checkbounds(...)` in 0.4. The 0.4-style two-argument form of `@boundscheck` is left unchanged.

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

## 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 All @@ -83,6 +85,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `redirect_stdout`, `redirect_stderr`, and `redirect_stdin` take an optional function as a first argument, `redirect_std*(f, stream)`, so that one may use `do` block syntax (as first available for Julia 0.6).

* `unsafe_get` returns the `:value` field of a `Nullable` object without any null-check and has a generic fallback for non-`Nullable` argument. [#18484](https://github.com/JuliaLang/julia/pull/18484) (Also, `isnull` has a generic fallback for non-`Nullable` argument.)

## Renamed functions

* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively.
Expand Down
22 changes: 22 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ function _compat(ex::Expr)
elseif VERSION < v"0.5.0-dev+4340" && length(ex.args) > 3 &&
istopsymbol(withincurly(ex.args[1]), :Base, :show)
ex = rewrite_show(ex)
elseif VERSION < v"0.6.0-dev.826" && length(ex.args) == 3 && # julia#18510
istopsymbol(withincurly(ex.args[1]), :Base, :Nullable)
ex = Expr(:call, f, ex.args[2], Expr(:call, :(Compat._Nullable_field2), ex.args[3]))
end
if VERSION < v"0.5.0-dev+4305"
rewrite_iocontext!(ex)
Expand Down Expand Up @@ -1657,4 +1660,23 @@ if VERSION < v"0.5.0-dev+4677"
Base.cholfact!(A::HermOrSym, T::Type) = cholfact!(A.data, Symbol(A.uplo), T)
end

# julia#18510
if VERSION < v"0.6.0-dev.826"
_Nullable_field2(x) = !x
else
_Nullable_field2(x) = x
end

# julia#18484
if VERSION < v"0.6.0-dev.848"
unsafe_get(x::Nullable) = x.value
unsafe_get(x) = x
export unsafe_get
if VERSION < v"0.4.0-dev+656" # so "nullable.jl" is included
isnull(x) = false
else
Base.isnull(x) = false
end
end

end # module
54 changes: 54 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1455,3 +1455,57 @@ for A in (Hermitian(randn(5,5) + 10I),
@test F[:U]'F[:U] ≈ A[F[:p], F[:p]]
@test F[:L]*F[:L]' ≈ A[F[:p], F[:p]]
end

types = [
Bool,
Float16,
Float32,
Float64,
Int128,
Int16,
Int32,
Int64,
Int8,
UInt16,
UInt32,
UInt64,
UInt8,
]
for T in types
# julia#18510, Nullable constructors
x = @compat Nullable(one(T), true)
@test isnull(x) === false
@test isa(x.value, T)
@test eltype(x) === T

x = @compat Nullable{T}(one(T), true)
y = @compat Nullable{Any}(one(T), true)
@test isnull(x) === false
@test isnull(y) === false
@test isa(x.value, T)
@test eltype(x) === T
@test eltype(y) === Any

x = @compat Nullable{T}(one(T), false)
y = @compat Nullable{Any}(one(T), false)
@test isnull(x) === true
@test isnull(y) === true
@test eltype(x) === T
@test eltype(y) === Any

x = @compat Nullable(one(T), false)
@test isnull(x) === true
@test eltype(x) === T

x = @compat Nullable{T}()
@test isnull(x) === true
@test eltype(x) === T

# julia#18484, generic isnull, unsafe_get
a = one(T)
x = @compat Nullable(a, true)
@test isequal(unsafe_get(x), a)

x = @compat Nullable{Array{T}}()
@test_throws UndefRefError unsafe_get(x)
end