Skip to content

Commit

Permalink
Drop compat code for Val(n) and ntuple/reshape with Val from #…
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed Sep 30, 2019
1 parent f9ff3b6 commit 7bd8d1b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ Currently, the `@compat` macro supports the following syntaxes:

* `isnothing` for testing if a variable is equal to `nothing` ([#29674]).

* `Val(x)` constructs `Val{x}()`. ([#22475])

* The `reshape` and `ntuple` APIs are extended to support `Val{x}()` arguments on 0.6 and below.

* `logdet` for `Number`s ([#22629]).

* `fieldcount` is equivalent to `nfields` for Julia versions 0.6 and below and is used to
Expand Down
11 changes: 0 additions & 11 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import Base.invokelatest

include("compatmacro.jl")

# https://github.com/JuliaLang/julia/pull/22475
@static if VERSION < v"0.7.0-DEV.843"
import Base: Val
(::Type{Val})(x) = (Base.@_pure_meta; Val{x}())
# Also add methods for Val(x) that were previously Val{x}
import Base: reshape
reshape{N}(parent::AbstractArray, ndims::Val{N}) = reshape(parent, Val{N})
import Base: ntuple
ntuple{F,N}(f::F, ::Val{N}) = ntuple(f, Val{N})
end

# https://github.com/JuliaLang/julia/pull/22629
if VERSION < v"0.7.0-DEV.848"
import Base: logdet
Expand Down
35 changes: 35 additions & 0 deletions test/old.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,38 @@ let c = `ls -l "foo bar"`
@test length(c) == 3
@test eachindex(c) == 1:3
end

# Val(x)
# 0.7
begin
local firstlast
firstlast(::Val{true}) = "First"
firstlast(::Val{false}) = "Last"

@test firstlast(Val(true)) == "First"
@test firstlast(Val(false)) == "Last"
end

# Reshape to a given number of dimensions using Val(N)
# 0.7
let
for A in (rand(Float64, ()), rand(2), rand(2,3), rand(2,3,5), rand(2,3,5,7)), N in (1,2,3,4,5,6)
B = @inferred reshape(A, Val(N))
@test ndims(B) == N
if N < ndims(A)
new_sz = (size(A)[1:N-1]..., prod(size(A)[N:end]))
elseif N == ndims(A)
new_sz = size(A)
else
new_sz = (size(A)..., ntuple(x->1, N-ndims(A))...)
end
@test size(B) == new_sz
@test B == reshape(A, new_sz)
end
end

# ntuple with Val(N)
# 0.7
@test @inferred(ntuple(x->1, Val(3))) == (1,1,1)
@test @inferred(ntuple(x->x, Val(0))) == ()
@test @inferred(ntuple(x->x, Val(5))) == (1,2,3,4,5)
35 changes: 0 additions & 35 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,41 +138,6 @@ let
@test vec([b for (a,b) in pairs(IndexCartesian(), A14)]) == [11,12,13,14]
end

# Val(x)
# 0.7
begin
local firstlast
firstlast(::Val{true}) = "First"
firstlast(::Val{false}) = "Last"

@test firstlast(Val(true)) == "First"
@test firstlast(Val(false)) == "Last"
end

# Reshape to a given number of dimensions using Val(N)
# 0.7
let
for A in (rand(Float64, ()), rand(2), rand(2,3), rand(2,3,5), rand(2,3,5,7)), N in (1,2,3,4,5,6)
B = @inferred reshape(A, Val(N))
@test ndims(B) == N
if N < ndims(A)
new_sz = (size(A)[1:N-1]..., prod(size(A)[N:end]))
elseif N == ndims(A)
new_sz = size(A)
else
new_sz = (size(A)..., ntuple(x->1, N-ndims(A))...)
end
@test size(B) == new_sz
@test B == reshape(A, new_sz)
end
end

# ntuple with Val(N)
# 0.7
@test @inferred(ntuple(x->1, Val(3))) == (1,1,1)
@test @inferred(ntuple(x->x, Val(0))) == ()
@test @inferred(ntuple(x->x, Val(5))) == (1,2,3,4,5)

# 0.7
@test read(IOBuffer("aaaa"), String) == "aaaa"
@test occursin("read(@__FILE__, String)", read(@__FILE__, String))
Expand Down

0 comments on commit 7bd8d1b

Please sign in to comment.