Skip to content

Add informative error when converting symbolic dimensions to Unitful #80

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/DynamicQuantitiesUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Base.convert(::Type{Unitful.Quantity}, x::DynamicQuantities.Quantity) =
validate_upreferred()
cumulator = DynamicQuantities.ustrip(x)
dims = DynamicQuantities.dimension(x)
if dims isa DynamicQuantities.SymbolicDimensions
throw(ArgumentError("Conversion of a `DynamicQuantities.Quantity` to a `Unitful.Quantity` is not defined with dimensions of type `SymbolicDimensions`. Instead, you can first use the `uexpand` function to convert the dimensions to their base SI form of type `Dimensions`, then convert this quantity to a `Unitful.Quantity`."))
end
equiv = unitful_equivalences()
for dim in keys(dims)
value = dims[dim]
Expand Down
5 changes: 5 additions & 0 deletions test/test_unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ x = 1.0u"scale"
@test typeof(x) <: Unitful.Quantity{Float64, MyScaleUnit.𝐒}
@test_throws ErrorException convert(DynamicQuantities.Quantity, x)
# These are not supported because there is no SI equivalency

# issue 79
symbolic = DynamicQuantities.us"s"
@test_throws ArgumentError convert(Unitful.Quantity, symbolic)
@test convert(Unitful.Quantity, DynamicQuantities.uexpand(symbolic)) == 1.0 * Unitful.u"s"