Skip to content

Commit 1d33d04

Browse files
authored
Add Val(x) (#381)
1 parent bdf61d8 commit 1d33d04

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ Currently, the `@compat` macro supports the following syntaxes:
143143

144144
* `@__MODULE__` is aliased to `current_module()` for Julia versions 0.6 and below. Versions of `Base.binding_module`, `expand`, `macroexpand`, and `include_string` were added that accept a module as the first argument. ([#22064])
145145

146+
* `Val(x)` constructs `Val{x}()`. ([#22475])
147+
146148
## Renamed functions
147149

148150

@@ -283,3 +285,4 @@ includes this fix. Find the minimum version from there.
283285
[#21257]: https://github.com/JuliaLang/julia/issues/21257
284286
[#21346]: https://github.com/JuliaLang/julia/issues/21346
285287
[#22064]: https://github.com/JuliaLang/julia/issues/22064
288+
[#22475]: https://github.com/JuliaLang/julia/issues/22475

src/Compat.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,12 @@ if VERSION < v"0.6.0-pre.beta.455"
607607
isless(x::Dates.OtherPeriod, y::Dates.FixedPeriod) = throw(MethodError(isless, (x, y)))
608608
end
609609

610+
# https://github.com/JuliaLang/julia/pull/22475
611+
if VERSION < v"0.7.0-DEV.843"
612+
import Base: Val
613+
(::Type{Val})(x) = (Base.@_pure_meta; Val{x}())
614+
end
615+
610616
include("deprecated.jl")
611617

612618
# https://github.com/JuliaLang/julia/pull/21746

test/runtests.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,28 +338,33 @@ end
338338
# Val
339339
begin
340340
local firstlast
341-
firstlast(::Type{Val{true}}) = "First"
342-
firstlast(::Type{Val{false}}) = "Last"
341+
firstlast(::Val{true}) = "First"
342+
firstlast(::Val{false}) = "Last"
343343

344-
@test firstlast(Val{true}) == "First"
345-
@test firstlast(Val{false}) == "Last"
344+
@test firstlast(Val(true)) == "First"
345+
@test firstlast(Val(false)) == "Last"
346346
end
347347

348+
# The constructors for some linear algebra stuff changed to take Val{x}
349+
# instead of Type{Val{x}}
350+
const valtrue = VERSION < v"0.7.0-DEV.843" ? Val{true} : Val(true)
351+
const valfalse = VERSION < v"0.7.0-DEV.843" ? Val{false} : Val(false)
352+
348353
# qr, qrfact, qrfact!
349354
let A = [1.0 2.0; 3.0 4.0]
350-
Q, R = qr(A, Val{false})
355+
Q, R = qr(A, valfalse)
351356
@test Q*R A
352-
Q, R, p = qr(A, Val{true})
357+
Q, R, p = qr(A, valtrue)
353358
@test Q*R A[:,p]
354-
F = qrfact(A, Val{false})
359+
F = qrfact(A, valfalse)
355360
@test F[:Q]*F[:R] A
356-
F = qrfact(A, Val{true})
361+
F = qrfact(A, valtrue)
357362
@test F[:Q]*F[:R] A[:,F[:p]]
358363
A_copy = copy(A)
359-
F = qrfact!(A_copy, Val{false})
364+
F = qrfact!(A_copy, valfalse)
360365
@test F[:Q]*F[:R] A
361366
A_copy = copy(A)
362-
F = qrfact!(A_copy, Val{true})
367+
F = qrfact!(A_copy, valtrue)
363368
@test F[:Q]*F[:R] A[:,F[:p]]
364369
end
365370

@@ -1397,11 +1402,11 @@ for A in (Hermitian(randn(5,5) + 10I),
13971402
@test istriu(chol(A))
13981403
@test chol(A) F[:U]
13991404

1400-
F = cholfact(A, Val{true})
1405+
F = cholfact(A, valtrue)
14011406
@test F[:U]'F[:U] A[F[:p], F[:p]]
14021407
@test F[:L]*F[:L]' A[F[:p], F[:p]]
14031408
Ac = copy(A)
1404-
F = cholfact!(Ac, Val{true})
1409+
F = cholfact!(Ac, valtrue)
14051410
@test F[:U]'F[:U] A[F[:p], F[:p]]
14061411
@test F[:L]*F[:L]' A[F[:p], F[:p]]
14071412
end

0 commit comments

Comments
 (0)