Skip to content
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

Closed
dsaf opened this issue Jan 29, 2015 · 8 comments
Closed

Implement Units of Measure or a broader class of similar contracts #144

dsaf opened this issue Jan 29, 2015 · 8 comments

Comments

@dsaf
Copy link

dsaf commented Jan 29, 2015

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.

@lyubomyr-shaydariv
Copy link

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)?

@manish
Copy link

manish commented Jan 29, 2015

@lyubomyr-shaydariv
I think you should be able to do. I haven't used F#, but I would love to have a feature like this.

e.g. if your first variable is a weight say 16kg or more scientifically a force 16N Newton and your second variable is length, say 5 meters, then when you multiply them, you get 80 Nm. A Nm is torque.

A variable distance say 20 miles divided by another variable 5 hours would yield speed, 4 miles/hour.

@AdamSpeight2008
Copy link
Contributor

10 is just value, it has no implicit meaning. 10 what?
10 metres is a better, as you know it that it is a different to say 10N
I think it also is aware that metres is unit of measure for distance and newtons is a unit of measure for mass

It prevents you doing silly things like adding metres to newtons , but you could be allow to multipy them for Nm (Torque).

Unit of Measure add an additional "compile-time" type safety to numeric values.
At the moment to sort of get the same functionality you have to implement a system yourself.

Example 0
Example 1

@dsaf
Copy link
Author

dsaf commented Jan 30, 2015

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.

@dsaf
Copy link
Author

dsaf commented Apr 29, 2015

From #2209:

I don't see why not. It would nice to have user-defined literals as in C++11, so you can do something like:

public primitive Radian
{public static Radian operator _rad;

   public static implicit operator Radian(int value)
   {
       return new Radian((value % 360) * Math.PI / 180f);
   }

   public static implicit operator Radian(float f)
   {
       // do the corresponding calculation here ...
   }
   ...
}

So when you use it like var myRadian = 45_rad; or var myRadian = 45.23f_rad;, the literal will pick the corresponding implicit operator to get the Radian;

@dsaf
Copy link
Author

dsaf commented May 7, 2015

A way of declaring unsigned floats would also be beneficial.

@omidkrad
Copy link

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.

@MadsTorgersen
Copy link
Contributor

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.

brittlism pushed a commit to brittlism/roslyntech that referenced this issue Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants