-
Notifications
You must be signed in to change notification settings - Fork 22
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
[Question] Why was .as<Unit, Rep>() deprecated? #121
Comments
Thanks for asking! First, I wanted to check something. Converting integral celsius temperatures to integral decikelvins is lossy, right? Since the additive constant is 273.15 K, the largest unit that could work would be [1/20] K. (I think we actually use So, were you OK with this lossy conversion? And if not, can you simply switch to constexpr CentiKelvins MAX_CHARGING_THRESHOLD = celsius(43).as(centikelvins);
// Or, more simply:
constexpr CentiKelvins MAX_CHARGING_THRESHOLD = celsius(43); (I'm assuming that your By the way --- if this was a lossy conversion, why did Au allow it? Answer: because the "explicit-Rep" form is morally equivalent to I've only recently begun to ponder whether we should someday separate the notion of "forcing" from the ability to specify a given Rep. I have always conflated these ideas so far, probably because Now for the stated question. 🙂 We deprecated the "CppCon style" interfaces, such as Let's say that
It's the same for replacing |
Thank you @chiphogg that makes a lot of sense. Personally I liked the CppCon style better because of my example above but it comes with a tradeoff as always. |
I wanted to say a little more for posterity about why we switched (especially for folks in the future who find this issue via search). "We didn't want to have two ways to spell the same thing" --- that's easy to see, but why pick one vs. the other? It turns out the new syntax actually has some concrete advantages. Better composabilityWith the old syntax, you need to have a type handy. Composing types is just generally really awkward (unfortunately, this is still true today). However, values --- whether // Old way: need to create some ad hoc types to represent unusual units.
{
using MetersPerSecondSquared = UnitPowerT<UnitQuotientT<Meters, Seconds>, 2>;
const auto vel_sq_m2 = vel_sq.in<MetersPerSecondSquared>();
}
// New way: compose the units on the fly!
{
const auto vel_sq_m2 = vel_sq.in(squared(meters / second));
} Note that this approach is also clearer: the name Less need for
|
In our code base we pass around temperatures in
uint32_t
deci-kelvins since it gives us the right amount of precision we want without requiring floating point type, and the relevant range is contained withinuint32_t
, so we defined:But most of the temperature constants are expressed in degrees-Celsius since that is what we usually receive from EE teams and in general more human friendly.
This creates a bit of annoyance when wanting to convert these Celsius constant to DeciKelvins, since a lot of data needs to be repeated:
Ideally I wish I could just write:
Since
DeciKelvins
already contains both the desired representation and the unit, everything is explicit and to point.Also the use of actual type feels more natural with
as
andin
as they are both act as some kind of casting mechanism.Was there a specific reason to deprecate
as<Unit, Rep>
?Would it be possible to bring it back, potentially with
as<Quantity<Unit,Rep>>
?Or maybe I'm looking at this all wrong and the idiomatic usage of this library avoids this problem altogether?
The text was updated successfully, but these errors were encountered: