SymbolicML / DynamicQuantities.jl

Efficient and type-stable physical quantities in Julia
https://symbolicml.org/DynamicQuantities.jl/dev/
Apache License 2.0
132 stars 17 forks source link

Behavior with `missing` #68

Closed jkrumbiegel closed 11 months ago

jkrumbiegel commented 11 months ago

When trying to integrate DynamicQuantities support into an existing code base, I noticed that one difficulty came from its different behavior regarding missing.

julia> missing * DynamicQuantities.us"hr"
(missing) hr

julia> missing * u"hr"
missing

The lower version is Unitful. Because missings are wrapped within Quantity in DynamicQuantities, a whole host of missing-handling functions doesn't work and quantities have to be unwrapped manually. As the simplest example, ismissing(missing * DynamicQuantities.us"hr") === false. What was the rationale for this behavior, could it still be changed?

MilesCranmer commented 11 months ago

Hi @jkrumbiegel,

Thanks for this note! I would consider this a bug because supporting missing-handling functions should be important, but hasn't really been considered in depth. So the current behavior should be changed.

Do you think you could create a suite of unittests for the behavior you are looking for? (Maybe in a PR.) Then I can add the missing functions to make them pass.

Cheers, Miles

MilesCranmer commented 11 months ago

Actually I think once #49 merges, this will already be working, because then AbstractQuantity <: Number and therefore the built-in functions for missing would already propagate!

jkrumbiegel commented 11 months ago

Oh, I see, then this behavior stems from Number. Great, that was the other thing that caused some headaches with integration. So I'll wait for #49 to merge before proceeding. The remaining problems were all due to zero not being defined on Type{Quantity} but that's expected.

MilesCranmer commented 11 months ago

Cool!

The remaining problems were all due to zero not being defined on Type{Quantity} but that's expected.

Yeah, unfortunately this is a fundamental difference between type-based units and value-based units. The solution is to switch to using zero(::Quantity) so it can see both the values and the types – this will work for both Unitful and DynamicQuantities.

gaurav-arya commented 11 months ago

Yeah, unfortunately this is a fundamental difference between type-based units and value-based units. The solution is to switch to using zero(::Quantity) so it can see both the values and the types – this will work for both Unitful and DynamicQuantities.

A nice analogy could be arrays/matrices also have the same behavior, requiring zero of the value since the size is not part of the type. This also means we can at least expect any functions that have been written to support array inputs (e.g. sum) to already get this right 🙂

MilesCranmer commented 11 months ago

Closed with v0.8!