-
Notifications
You must be signed in to change notification settings - Fork 391
Quantity Types with only a single unit #435
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
Comments
Hi, I assume this is the context of some sort of dynamic unit conversion app similar to our sample app; In that context, you are right there is nothing built-in to help filter this. What you do is perfectly fine I think. One improvement could be to let UnitsNet provide a mapping like this: Such a method could be added to our static, generated The reason single-unit quantity types exist is to provide semantics for those types of values in an application with its own type, instead of passing Hope this helps! |
I am sorry if I was unclear on the purpose. You are correct. I have created in Winforms a similar app to your WPF sample, and made it available to my staff to play with. They were of the opinion that it was pointless having entries with only a single unit, because it can do nothing other than clutter the UI. Because there is the chance that other units and quantity types will be added in the future, we definitely do not want to use any sort of hard coded switch statement such as shown in your code. The problem is that it would have to be rearchitected for each addition, which is hardly ideal I am sure you will agree. The code I provided has the major advantage that it does not need to be changed no matter how many units or quantity types there are in the library. For this reason I am not in favour of your proposed solution. Here is how I currently populate the QuantityType list:
This does not care what content the library has, it will function regardless. However, I would have preferred not to need to iterate the enum to retrieve the entries I want to include. It works perfectly as it is, I just wondered if there was a better way :-) |
Ah, sorry for not being clear myself. My proposal was intended for how to implement it in The advantage is that consumers don't need to mess with reflection (which may break with a future release of UnitsNet) and have a simple |
In that case, it would definitely be better for the consumer if that were added, and it would have the advantage of removine the need for reflection as you say, but it would still leave us having to iterate to find out which QuantityTypes are capable of offering any form of conversion. The ideal solution would be something like:
I understand that this is not available, and probably not practical given your architecture, but you can see that it would be easy for the consumer. |
This particular method I think belongs in an application. It is an application-specific need to know that a certain quantity only has a single unit for the purpose of filtering them. Also it is trivial to code this part so I don't think we will add it to UnitsNet. Enumerating the units, however, does have some value in including in the library and I'm happy to help you bring it into a pull request if you care to pursue that. |
I am not sure I entirely agree with your comments, but I certainly understand and respect them. For me, being able to retrieve only valid or usable objects contained within it seems like the responsibility of the library and not the application accessing it which should just be able to say to the library 'give me only the things I can actually use' - not least because the ones with a single unit are useless in any environment with our without a UI - but I can see why you consider it differently. It is also worth noting that currently if you pass in QuantityType.Undefined during your iteration you get an exception rather than a graceful exit, and with your method we could pass back an empty array rather then throwing an exception assuming that is good practice as far as your library is concerned. It would be interesting to pursue the unit enumeration though (noting the TODO note in the sample application), and I will certainly try to fit it into my schedule. If you can give me some guidance it would be appreciated as I am entirely new to github having initially joined purely so that I could ask this question! |
And I respect you being able to disagree in a good way :-) I don't mean to be a stick in the mud, my intention is only to keep the library small, focused and solve a specific purpose - represent physical quantities and convert between units of a quantity. We also struggle a bit with binary size, so I hesitate to add anything I consider unnecessary.
I still prefer the Pull requestI'm happy to help, let me know what you need. Summarized: create a fork of UnitsNet on github, clone that fork locally with git, checkout a branch See https://help.github.com/articles/creating-a-pull-request-from-a-fork/ If you agree with my proposal, I think it's a matter of:
P.S: |
Hey, it's your library! I am just grateful to have the use of the fruits of your labour... |
Hi, do you have any plans to pursue this? Type unitEnumType = UnitEnumTypes.First(t => t.FullName == $"UnitsNet.Units.{quantity}Unit");
return Enum.GetValues(unitEnumType).Length > 2; UnitSystem is split into new types in v4 branch, UnitAbbreviationsCache is relevant for this change:
Let me know if you want to proceed with a pull request on this or if we should close the issue. |
Hi Andreas,
I did have plans to follow it up, but unfortunately my disabled partner has had sepsis twice so I have been spending a lot of time looking after our grandchildren and chasing round hospitals whilst still trying to fit work in… Regrettably playing with your UnitsNet kind of dropped down my priority list.
I would like to follow it up, but I am not sure when I will have the time. For a start, you are doing things completely differently to the way I have seen any other .net project managed, so I would need to learn about the tools and methods you are using before I could reasonably help.
If you are willing to wait, I am happy to try to do this when I can. Otherwise, please close the ticket and perhaps we can revisit at a later date.
Chris Bray
…--
Synergy Accounts - Intelligent Accounting and Business Management Software
89 Railway Road, Newhaven, East Sussex. BN9 0AY
Tel: 0345 193 0617 (Included in call packages, otherwise charged the same as 01 and 02, multiple lines)
Email: chrisbray@symergyaccounts.com<mailto:chrisbray@symergyaccounts.com>
Website: www.synergyaccounts.com<http://www.synergyaccounts.com>
Synergy Accounts Ltd
Registered in England and Wales No: 05345330 Registered Office: 48 Chichester Road, Seaford, BN25 2DL
VAT Registration No: GB 177 3548 74
From: Andreas Gullberg Larsen [mailto:notifications@github.com]
Sent: 15 October 2018 18:59
To: angularsen/UnitsNet <UnitsNet@noreply.github.com>
Cc: support@synergyaccounts.com; Author <author@noreply.github.com>
Subject: Re: [angularsen/UnitsNet] Quantity Types with only a single unit (#435)
Hi, do you have any plans to pursue this?
If I read correctly, what remains is to add a way to dynamically query the units of a quantity - something like Array unitEnumValues = UnitAbbreviationsCache.Default.GetUnits(QuantityType);. It would remove the need for reflection in your initial example
Type unitEnumType = UnitEnumTypes.First(t => t.FullName == $"UnitsNet.Units.{quantity}Unit");
return Enum.GetValues(unitEnumType).Length > 2;
UnitSystem is split into new types in v4 branch, UnitAbbreviationsCache is relevant for this change:
https://github.com/angularsen/UnitsNet/blob/86b2efcef08ae73f9310e77560064475383890db/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs#L52
Let me know if you want to proceed with a pull request on this or if we should close the issue.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#435 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AlMbokwPGTjV7FnGuGqocL-c0mHBWOlhks5ulMzngaJpZM4Tzi6w>.
|
Ouch, I'm terribly sorry to hear that :-/ I hope your partner recovers soon! If there is anything I can help you with to get you started, let me know. We do have a wiki that explains steps for adding new units, but other than that it should be fairly normal C# code. The only special thing perhaps is that we use a lot of code generation with PowerShell scripts. For this change you should not have to worry about it, but you run
|
I am closing this issue due to inactivity. Please reopen when there is renewed interest. Also please see the PR #576 that improves working dynamically with quantities and things like converting and enumerating units. |
Unless I am missing something, there are a number of conversions that can have a value of only 1:1 because there is only one entry in the list of available conversions. An example would be QuantityType.ElectricCharge, which has only undefined and Coulomb as available units. Clearly if I am correct then until more units are added this quantity type is rendered entirely pointless.
At the moment there appears to be no easy way to work out whether or not to include each QuantityType in the list, i.e. to exclude those where conversion is not currently possible and thereby reduce pointless clutter in any UI.
For the moment I have simply added a 'Can Convert' method:
The question is, is there a better way than this to filter the list of quantity types to only those which are capable of converting something?
The text was updated successfully, but these errors were encountered: