Convenience functions for vals#23658
Convenience functions for vals#23658bramtayl wants to merge 6 commits intoJuliaLang:masterfrom bramtayl:patch-1
Conversation
|
|
||
| Val(x) = (@_pure_meta; Val{x}()) | ||
|
|
||
| eltype(::Val{T}) where T = T |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Hmm maybe eltype isn't the right name for what I want. I just want a function that will turn Val{:a}() into :a
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
Val isn't a collection or the type of a collection so eltype is not appropriate here.
There was a problem hiding this comment.
I mean I could just leave it out this PR, or choose another name? devalue?
base/essentials.jl
Outdated
| eltype(::Val{T}) where T = T | ||
|
|
||
| (&)(v1::Val{true}, v2::Val{true}) = Val{true}() | ||
| (&)(v1::Val, v2::Val) = Val{false}() |
There was a problem hiding this comment.
these methods seem to cover too many possibilities; I'd suggest to just write out all four combinations
vtjnash
left a comment
There was a problem hiding this comment.
Val shouldn't be used with booleans or places where you want to do computations.
|
Ive been using value algebra for significant performance gains in places like JuliennedArrays |
Why |
|
See previous attempts at doing this (#22518), the manual (https://docs.julialang.org/en/stable/manual/performance-tips/#Types-with-values-as-parameters-1), the equivalent discussions on lifting Nullablility, and discourse. |
|
Sounds as if there is indeed a specific use case: |
|
No, that is saying the opposite: Val is useful for cases where you don't need to do arithmetic on it. |
|
I'm not using this these functions to do arithmetic though... |
|
If by arithmetic you mean repeated functions on different values. Instead, I'm doing the same function on the exact same list of values repeatedly. |
|
I agree with @vtjnash that these cases should be handled by compiler constant folding instead of manually using |
|
It doesn't seem to be working though? |
|
My follow-up pull request was going to be a type stable |
|
Both of which will be needed for a type-stable mapslices. |
|
I've also worked on this before, and I had trouble finding a way to get constant folding to work in the way that's necessary for a package like @bramtayl's JuliennedArrays (which already works well). I've also read the manual and any relevant issues/discourse discussions that I can find, and I'm very interested to learn what is the proper way to do this with Julia v0.7 master. |
|
Constant folding seems very incomplete. Here's another example where constant folding is not working as expected so value wrapping is necessary: |
|
Am I correct in my understanding that constant propagation cannot survive passing through functions not known to the compiler as pure? If so, it severely limits the usefulness of constant propagation. |
|
My understanding is that this is a non-issue with the now aggressive constant propogation |
No description provided.