JuliaPhysics / JuliaPhysics/DynamicQuantities.jl
Feature request: dimension helper functions?
- Dominant language
- Julia
- Stars
- 155
- Forks
- 32
- PR merge metrics
- No merged PRs in 30d
Description
Hi Miles, just following up on my evil master plan for JuliaAstro, would you be open to a PR adding some convenience `is` functions?
I'm not sure if this has been discussed somewhere already, but we're basically looking for a convenient way to dispatch on dimensions, e.g.,
```julia-repl
julia> using DynamicQuantities
julia> x = 3u"m"
3.0 m
julia> v = 3u"m/s"
3.0 m s⁻¹
julia> function do_stuff(q::UnionAbstractQuantity)
if islength(q)
#
elseif isspeed(q)
#
# ...
else
end
end
```
I see that this could be done by factoring out some of the [dimension display logic here](https://github.com/JuliaPhysics/DynamicQuantities.jl/blob/dfba00cb164b44ef610e1c677a683a4e57a6c524/src/utils.jl#L328-L339), to something like:
```julia-repl
julia> function islength(q::UnionAbstractQuantity)
d = dimension(q)
dd = filter(k -> !iszero(d[k]), keys(d))
return dd == (:length,) && d.length == 1
end
islength (generic function with 1 method)
julia> function isspeed(q::UnionAbstractQuantity)
d = dimension(q)
dd = filter(k -> !iszero(d[k]), keys(d))
return dd == (:length, :time) && d.length == 1 && d.time == -1
end
isspeed (generic function with 1 method)
julia> islength(3u"m")
true
julia> isspeed(3u"m/s")
true
```
Alternatively, would this make sense as a trait that could be dispatched on at the type level, or would this run counter to the dynamic runtime design here?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.