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

Backport reshape and ntuple Val() APIs to 0.5 and 0.6 #399

Merged
merged 3 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Cmd` elements can be accessed as if the `Cmd` were an array of strings for 0.6 and below ([#21197]).

* `Val(x)` constructs `Val{x}()`. ([#22475])
* `Val(x)` constructs `Val{x}()`. The `reshape` and `ntuple` APIs are extended to support `Val{x}()` arguments on 0.6 and below. ([#22475])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you put these on separate bullets?


* `chol` and `chol!` for `UniformScalings` ([#22633]).

Expand Down
5 changes: 5 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ end
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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be reshape(parent, Val(N)) since Val{N} is the type and giving reshape the Val type is deprecated in favor of giving it the Val object?

Copy link
Member Author

@mbauman mbauman Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding a method to support reshape(parent, Val(N)) on 0.6 and 0.5. Those versions have this functionality, but it's defined in terms of reshape(parent, Val{N}). So we just convert the new API to speak with the old API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Sorry for the noise.

import Base: ntuple
ntuple{F,N}(f::F, ::Val{N}) = ntuple(f, Val{N})
end

# https://github.com/JuliaLang/julia/pull/22629
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,30 @@ begin
@test firstlast(Val(false)) == "Last"
end

# Reshape to a given number of dimensions using Val(N)
# 0.7
let
for A in (rand(()), 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)

# @nospecialize
# 0.7
no_specialize(@nospecialize(x)) = sin(1)
Expand Down