Skip to content
Closed
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
18 changes: 18 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,24 @@ end

Val(x) = (@_pure_meta; Val{x}())

eltype(::Val{T}) where T = T
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't correct. T can be Array{S,N}, in which case eltype(Val{Array{S,N}}) should be S. I'd instead make this eltype(::Val{T}) where {T} = eltype(T).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm maybe eltype isn't the right name for what I want. I just want a function that will turn Val{:a}() into :a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

However, might be consistent with this:

Julia-0.7.0-DEV> x = Vector{Array{Int,3}}()
0-element Array{Array{Int64,3},1}

Julia-0.7.0-DEV> eltype(x)
Array{Int64,3}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Val isn't a collection or the type of a collection so eltype is not appropriate here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I mean I could just leave it out this PR, or choose another name? devalue?


(&)(v1::Val{false}, v2::Val{false}) = Val{false}()
(&)(v1::Val{true}, v2::Val{false}) = Val{false}()
(&)(v1::Val{false}, v2::Val{true}) = Val{false}()
(&)(v1::Val{true}, v2::Val{true}) = Val{true}()

(|)(v1::Val{false}, v2::Val{false}) = Val{false}()
(|)(v1::Val{true}, v2::Val{false}) = Val{true}()
(|)(v1::Val{false}, v2::Val{true}) = Val{true}()
(|)(v1::Val, v2::Val) = Val{true}()

!(::Val{false}) = Val{true}()
!(::Val{true}) = Val{false}()

ifelse(switch::Val{false}, new, old) = old
ifelse(switch::Val{true}, new, old) = new

# used by interpolating quote and some other things in the front end
function vector_any(@nospecialize xs...)
n = length(xs)
Expand Down
18 changes: 18 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,21 @@ Base.:(<)(x::TypeWrapper, y::TypeWrapper) = (x.t <: y.t) & (x.t != y.t)
@test TypeWrapper(Int) <= TypeWrapper(Real)
@test !(TypeWrapper(Int) <= TypeWrapper(Float64))
end

vf = Val(false)
vt = Val(true)
@testset "value_logic" begin
@test vf & vf == vf
@test vf & vt == vf
@test vt & vf == vf
@test vt & vt == vt
@test vf | vf == vf
@test vf | vt == vt
@test vt | vf == vt
@test vt | vt == vt
@test !vf == vt
@test !vt == vf
@test ifelse(vf, vt, vf) == vf
@test ifelse(vt, vt, vf) == vt
@test eltype(vt)
end