Skip to content

Allow calling units with equivalences (partial apply) #21

Description

@abhro

One nice feature of Unitful is that units are callable, acting like shortcuts to uconvert. Would it be possible to add a method to units to make units callable with an equivalence? This would be helpful for shorter conversion calls.

Current:

julia> using Unitful, UnitfulEquivalences

julia> lambda = 300u"nm";

julia> uconvert(u"Hz", lambda, Spectral())
9.993081933333334e14 Hz

julia> Unitful.c/lambda |> u"Hz"
9.993081933333334e14 Hz

Proposed:

julia> lambda |> u"Hz"(Spectral())
9.993081933333334e14 Hz

This could be easily implemented by something like:

function (unit::Unit)(eq::Equivalence)
    return quantity -> uconvert(u, quantity, eq)
end

The syntax is not the cleanest (suggestions welcome!), but like Unitful, it allows the quantity in question to be front and center and the latter parts of the statement being conversion arithmetic.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions