-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
Hi Miles, just following up on my evil master plan for JuliaAstro, would you be open to a PR adding some convenience is<dimension> 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> 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)
# <do length stuff>
elseif isspeed(q)
# <do speed stuff>
# ...
else
end
endI see that this could be done by factoring out some of the dimension display logic here, to something like:
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")
trueAlternatively, 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?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels