I have a function where I want to parse a Unitful.Quantity
argument provided by the user for only the Time
dimensioned unit. For example…
f(1u"km/s") # should return u"s"
f(1u"km/d") # should return u"d"
Is this supported? I have some parameters
crimes below that get me close, but the retuned value is of type Unitful.Unit{:Second, 𝐓}
, not Unitful.FreeUnits{(s,), 𝐓, nothing}
.
julia> using Unitful
julia> function crime(vel::Unitful.Velocity)
# Flip the units so `Time` is in the numerator
TL = inv(unit(vel))
# Now we need to find which index (1 or 2) contains the Time unit
timeaxis = findfirst(T -> T isa Unitful.Dimension{:Time}, collect(typeof(typeof(TL).parameters[2]).parameters[1]))
# Now that we have the proper index, let's select the time unit
timeunit = typeof(TL).parameters[1][timeaxis]
end
julia> crime(1u"m/s") |> typeof
Unitful.Unit{:Second, 𝐓}
julia> u"m" / crime(1u"m/s")
ERROR: MethodError: no method matching /(::Unitful.FreeUnits{(m,), 𝐋, nothing}, ::Unitful.Unit{:Second, 𝐓})
Closest candidates are:
/(::Unitful.Units, ::Union{Dates.Day, Dates.Hour, Dates.Microsecond, Dates.Millisecond, Dates.Minute, Dates.Nanosecond, Dates.Second, Dates.Week}) at /Users/joey/.julia/packages/Unitful/PcVKX/src/dates.jl:91
/(::Unitful.Units, ::Unitful.MixedUnits) at /Users/joey/.julia/packages/Unitful/PcVKX/src/logarithm.jl:111
/(::Unitful.Units, ::Unitful.AbstractQuantity) at /Users/joey/.julia/packages/Unitful/PcVKX/src/quantities.jl:51
...
Stacktrace:
[1] top-level scope
@ REPL[10]:1
julia>