Skip to content

Why don't we have a base class for quantities? #308

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

Closed
0xferit opened this issue Nov 7, 2017 · 6 comments
Closed

Why don't we have a base class for quantities? #308

0xferit opened this issue Nov 7, 2017 · 6 comments

Comments

@0xferit
Copy link
Contributor

0xferit commented Nov 7, 2017

Wouldn't it be beneficial to move common things in quantity classes to a quantity base class?

Sometimes we don't know which quantity we are dealing with, but we want to print them, or get abbreviations etc. which is common in all quantities. This use case is important especially in GUI development.

What are your thoughts?

@angularsen
Copy link
Owner

I agree, I've just been waiting for the right real-world usecase and the right person with the motivation to get it done - solving a real need.
Could you please detail an example/pseudo code where having a base class or interface would simplify things?

@0xferit
Copy link
Contributor Author

0xferit commented Nov 10, 2017

I am planning to give you a proposal in detail when I have time, but for instance I remember we had to do reflection hack because of lacking a base Unit class.

For example, we have a list view control which lists (prints their name, or abbreviation) units of a given quantity. However, as we don't know which quantity we are dealing with, we want a List like this: List<Unit> which is not possible.

So instead, we had to do List<object> and lots of reflection and casting.

Similarly we could keep a List of quantities like: List<Quantity> if we had Quantity class.

To be able to demonstrate my arguments easily I'm working on a converter app. I'll share it soon.

@eriove
Copy link
Contributor

eriove commented Nov 10, 2017

Since the quantities are structs I've been worried by the boxing overhead of using an interface, but if the alternative is using an object and reflection the overhead of boxing will still be an improvement to what we have today and there wouldn't be a difference for existing code that uses the structs directly.

@angularsen
Copy link
Owner

@ferittuncer Looking forward to the pseudo code, as generally in these cases I have found that it's unnecessary to work "blindly" with the units. You usually know what quantity the units are for since you typically want to be able to convert to other units at some point. There is also a QuantityType enum type you can use to associate the values with a quantity and there is UnitConverter that can be used to dynamically convert between units based on unit names/abbreviations and a quantity name, but I'll await an example to comment further.

@angularsen
Copy link
Owner

angularsen commented Nov 11, 2017

@eriove Right, and I guess performance is perhaps not the primary focus if you have already resorted to reflection to achieve this :-) If I was writing performance sensitive code, I would not use Units.NET, but rather custom types tailored for my need.

@angularsen
Copy link
Owner

Closing this in favor of #371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants