-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Implement Units of Measure or a broader class of similar contracts #144
Comments
I'm not familiar with this concept, but is the following assumption true: if I declare one float as "weight" and another one as "length", then I can't perform any arithmetic operations over them or mutual assignments until I cast them back to floats (just strip the units)? |
@lyubomyr-shaydariv e.g. if your first variable is a weight say 16kg or more scientifically a force 16N A variable distance say 20 miles divided by another variable 5 hours would yield speed, 4 miles/hour. |
It prevents you doing silly things like adding Unit of Measure add an additional "compile-time" type safety to numeric values. |
Yes, otherwise you will need to apply the 'pure OOP' approach and create a struct per type of unit (or some type of generic wrapper) and then create all the operator overloads and then consuming code will have to deal with that type hierarchy and its performance implications. Basic value types are kind of a gap in the type system. |
From #2209:
|
A way of declaring unsigned floats would also be beneficial. |
I really like what @JohanLarsson has done in Gu.Units. It's probably the closest thing to F# we can do in C# at this time. I think this could be extended to support currencies as well. You don't want to add up USD and EUR, for example, before some conversion is made. |
Units of measure in F# are immensely cool. However, I think a similar thing in C# would be a major effort, that would benefit only a small portion of its developers. This is not the language's sweet spot. |
F# is the inspiration:
https://msdn.microsoft.com/en-us/library/dd233243.aspx
This could be user-configurable, however:
"Units of measure are used for compile-time unit checking but are not persisted in the run-time environment. Therefore, they do not affect performance."
"Units of measure are used for static type checking. When floating point values are compiled, the units of measure are eliminated, so the units are lost at run time. Therefore, any attempt to implement functionality that depends on checking the units at run time is not possible. For example, implementing a ToString function to print out the units is not possible."
Perhaps it could be an additional feature to the ones already requested such as method pre- and post- conditions etc.
The text was updated successfully, but these errors were encountered: