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

INVALID vs error #16

Closed j-fu closed 1 year ago

j-fu commented 1 year ago

Hi,

playing around with linear system solution with DynamicQuantities. Getting stuck a the point I already anticipated, so opening this issue:

Adding a meter and a kg in the moment results in INVALID. But this is quite hard to debug - one would dive into the code to figure out where this happens.

Wouldn't it be better to just throw an error on such an operation ? Or can throwing an error be made optional ?

Also it would be possible to remove the valid flag and to use its space for other purposes.

MilesCranmer commented 1 year ago

Hm, maybe you are right. I'm considering whether I should just change to throwing some sort of DynamicQuantities.DimensionError. What do you think?

The original motivation of using a flag for this is because having to use try; catch can be fairly expensive in Julia: https://discourse.julialang.org/t/performance-of-hasmethod-vs-try-catch-on-methoderror/99827. This approach is inspired by https://github.com/JuliaPreludes/Try.jl

But perhaps my use-case for this is niche, and it would be more useful for larger community to just throw errors. I can definitely see how this style could lead to bugs where you aren't sure where the valid=false is coming from; whereas throwing regular errors would easily highlight the problem. So perhaps it is worth it to just throw an error.