-
Notifications
You must be signed in to change notification settings - Fork 0
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
Will this design work out? #2
Comments
There are some good ideas here, my favorite being the ability to do mathematics using the dimension types (e.g. There's nothing inherent in Unitful that prevents the simplifications you prefer. A robust simplification mechanism is planned at some point or another. There's a PR I have been meaning to address for a while now that I should really do soon and your feedback is helpful to let me know that simplification should be a priority. I'm not sure I understand your type hierarchy. You have e.g. A few comments on what you say in your README file:
Fundamentally, the approaches for all type-based unit packages end up converging even if there are some details in implementation. The unit information needs to be encoded in the type signature, possibly with dimensional information if it isn't clear from the allowed units, and then you need to specialize on the numeric type. There's no way around that, I think. Since there's already a lot of adoption for both Unitful and SIUnits, I think it makes more sense to open some PRs in either place rather than reinvent the wheel. When I wrote Unitful I was trying to solve specific problems that could not be addressed without some serious changes to SIUnits: arbitrary unit support, better handling for overflows (e.g. Keno/SIUnits.jl#22), fractional exponents (e.g. Keno/SIUnits.jl#32), and so on. I guess my advice would be to clarify what fundamental problem you are solving by writing a new units package, and why it cannot be addressed by smaller changes to existing packages. But don't let me stop you from experimenting, you've already found a good idea with the |
For sure. I'm not really wanting to write my own package so much as just experiment. I'd like to see Unit support become part of Base eventually anyway because Units are so fundamental. I think the only reason no language has them built in is because they are hard to do well.
I think of a unit as being a type and a quantity as being an instance of a unit. So in my implementation I would call
I use "Dimension" to mean primitive unit. The wikipedia article on SI Units says "The SI base units form a set of mutually independent dimensions". Which to me indicates "dimension" means "base unit". But the wikipedia article on dimensions seems to use the word a bit differently and I think more in line with the way you use it. I think I will rename
Ah yes this is critical. I avoided this because it causes a proliferation of
Honestly I just can't wrap my head around Unitful's implementation. So it's really hard for me to provide good feedback. How would you implement my |
@ajkeller34 I'd like to see what you think of this approach to units. You know a lot more about scientific units and Julia than I do which makes me worried I'll waste your time but still I do feel like I'm on to something.
I was motivated to experiment with this because I really struggle to understand how all of Unitful's types fit together and honestly I still don't really. Maybe it's just me though.
And I'm encouraged by it handling some things better than Unitful. I'm not sure if it's anything inherent in Unitful's design that stops it handling these cases but I'll list them anyway for you to decide.
1kg/m * 1cm == 1 kg cm m^-1
, with this package1kg/m * 1cm == 0.01kg
. I'm not sure if Unitful's answer is equivalent or not because my algebra is a bit 🥔 but it definitely doesn't look as nice.1mm + 1cm == 11//1000 m
, with this package1mm + 1cm == 11mm
. This case is definitely just vanity though I know.About the design
Dimension
Exponent{n,Dimension}
Combination{Tuple{Exponent}
Whenever you do any math with units it will convert all units to
Combination
's. Perform the math then convert the result back down to the simplest type possible. So(1m*1m)::Length^2
and(1m²/1m)::Length
. This means that you only have to define one math method to support all kinds of units; like Unitful. While only ever exposing one definition of a certain type to the user. And that type might be one they themselves defined.Currently it will be really slow but I think generated functions can solve that just as they did with Unitful
The text was updated successfully, but these errors were encountered: