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

Feature request (or help request) #219

Closed
Lucretiel opened this issue Nov 8, 2020 · 5 comments
Closed

Feature request (or help request) #219

Lucretiel opened this issue Nov 8, 2020 · 5 comments

Comments

@Lucretiel
Copy link
Contributor

Is it possible to instantiate a Quantity with a known type but a runtime unit of measure? For instance, suppose I have the float value 2.5 and the string "km". Is there any way to parse that aside from formatting the quantity back into a string and using FromStr? If not, this is a feature request from a constructor or an enum of different unit measures of a each type that can be parsed and used to construct quantities.

@iliekturtles
Copy link
Owner

There isn't a way to do this right now and two issues prevent it: there isn't a way to just parse "km" into the appropriate unit and there is no new constructor that can take a unit at runtime. I just added #220 to resolve the former and #91 is open to resolve the later. The recent merge of #215, which added the Unit enum, should make both of these possible.

Let me know if the resolution of those two issues will resolve your problem so we can mark this issue complete.

@Lucretiel
Copy link
Contributor Author

Yup, those will definitely cover it!

@Lucretiel
Copy link
Contributor Author

I suppose a stopgap solution would be to parse "1 {unit}" and multiply by the untyped quantity

@iliekturtles
Copy link
Owner

Marking this issue closed since the other two cover the functionality needed. Please re-open if I missed anything!

@Lucretiel
Copy link
Contributor Author

Lucretiel commented Nov 28, 2020

For those finding there way here looking for a way to do this before the features land, I've found that format!("1.0 {unit}", unit=unit_str).parse() works perfectly.

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

2 participants