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

Better use of abbreviated unit identifiers #196

Open
JohelEGP opened this issue Oct 12, 2018 · 15 comments
Open

Better use of abbreviated unit identifiers #196

JohelEGP opened this issue Oct 12, 2018 · 15 comments
Milestone

Comments

@JohelEGP
Copy link
Contributor

For a long while now, I've thought that it'd be great if we could naturally write the following.

using namespace units::literals;
using namespace units::constants;
const auto speed_limit = 90_km/h;

However, we need to write 90_km/1_h as the abbreviated unit identifiers happened to be taken up by some "tags."

What do you think about repurposing these identifiers to somewhere else, like the units::constants namespace, where they'd be declared along the lines of:

inline constexpr time::hour_t<int> h(1);

This would allow us to more naturally express units in our programs. The standard time library already allows expressing dates using the natural syntax, like 1970y/January/1. I don't see why we shouldn't follow suit.

@nholthaus
Copy link
Owner

nholthaus commented Oct 26, 2018

Yes, this. It never crossed my mind honestly but I love the natural syntax. That's the ethos and purpose of the library in the first place.

Maybe the literals namespace is better than constants though?

@JohelEGP
Copy link
Contributor Author

I had thought about it, and people using namespace units::literals; for the UDLs might find themselves having undesired clashes or accidentally using these kind of "constant literals". However, I haven't come up with something better.

@nholthaus
Copy link
Owner

nholthaus commented Oct 26, 2018 via email

@JohelEGP
Copy link
Contributor Author

Yeah. literals might be good enough most of the time.

@nholthaus
Copy link
Owner

nholthaus commented Oct 26, 2018 via email

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 7, 2020

#256 removes the abbreviation-named tags.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 7, 2020

#212 blocks a general solution for this.

@nholthaus
Copy link
Owner

all the concentration unit names were expanded, so only dB uses an abbreviation. I'm pretty sure we don't want to use operators like / and * to change linear scales, since arithmetic has a different meaning in log space.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 7, 2020

Alternatively, dB should be pluralized, like how all other units were renamed (I don't know if there are others). Then dB would be freed up for this.

@nholthaus
Copy link
Owner

I don't have a problem with pluralizing the names, but I don't think it will work as intended. I can't do 20 * dB * W because operator* isn't defined for decibel_scale types, and I think even if it did that would still result in watts?

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 7, 2020

Right. Let's leave non-linear scales off this.

@nholthaus
Copy link
Owner

I think we're good to go then, I really want this as part of the release.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 8, 2020

Well, it should be easy to implement, given that only arithmetic types are supported at the moment.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 9, 2020

I have an initial implementation: https://github.com/johelegp/units/tree/natural_syntax. But the restriction of the underlying type being an arithmetic type gets in the way. It'd be easier if there was support for generalized underlying types.

@nholthaus
Copy link
Owner

totally agree. I think the criticisms against being able to use std::complex are particularly valid.

I don't think there was any additional rationale for arithmetic underlying types than that it was obvious and a trait was readily available. I might have thought it was advantageous for trivial units, but I forget the motivation behind that. Probably some constexpr limitation in older compilers.

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

2 participants