Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Allow for More Flexible Unitful.jl Constructions #35

Closed
bhgomes opened this issue Apr 17, 2019 · 4 comments · Fixed by #36
Closed

Feature Request: Allow for More Flexible Unitful.jl Constructions #35

bhgomes opened this issue Apr 17, 2019 · 4 comments · Fixed by #36

Comments

@bhgomes
Copy link

bhgomes commented Apr 17, 2019

I think it would be great if it were possible to write:

using Measurements, Unitful, Unitful.DefaultSymbols
quantity = 1.3m ± 2.01μm

instead of

quantity = (1.3 ± 2.01e-6)m

The Julia REPL says that it would require a measurement(::Quantity, ::Quantity) function.

@giordano
Copy link
Member

You can achieve what you'd like with the following definitions:

using Measurements, Unitful

function Measurements.measurement(a::T, b::T) where {T<:Quantity}
    u = unit(a)
    return measurement(ustrip(u, a), ustrip(u, b)) * u
end

Measurements.measurement(a::Quantity{T1,D,U1}, b::Quantity{T2,D,U2}) where {T1,T2,D,U1,U2} =
    measurement(promote(a, b)...)

E.g.

julia> quantity = 1.3km ± 2.01μm
1300.0 ± 2.01e-6 m

However I'm not going to add this feature because I don't wont to depend on Unitful.jl

@bhgomes
Copy link
Author

bhgomes commented Apr 18, 2019

I don't see why this couldn't be added as an optional function if Unitful.jl is already installed in the user's environment. This wouldn't add to your dependencies and it would be obviously helpful for anyone using both packages.

@giordano
Copy link
Member

giordano commented Apr 18, 2019

Yesterday I started playing to see if this feasible without using Unitful.jl: https://github.com/JuliaPhysics/Measurements.jl/tree/unitful. However I won't have much time to continue this experiment before next week

@giordano
Copy link
Member

giordano commented Apr 18, 2019

I don't see why this couldn't be added as an optional function if Unitful.jl is already installed in the user's environment

This isn't really straightforward without using some magic. One possible magic is using the Requires.jl which we happen to already load for something else. I'm going to open PR #36 to fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants