Skip to content

Feature request: dimension helper functions? #197

@icweaver

Description

@icweaver

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
       end

I 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")
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions