-
Notifications
You must be signed in to change notification settings - Fork 86
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
Give access to the underlying number as an lvalue reference #476
Comments
|
I can restore an lvalue overload of |
I suppose we'll just have to |
I'm not a big fan of the interface, because it's not unit-safe: it doesn't force users to name the unit explicitly at the callsite. Au has a unit-safe version of this:
|
Well, @JohelEGP just provided one of many use cases for this feature 😉 For cases where we want to be "unit-safe" we have: mp-units/src/core/include/mp-units/quantity.h Lines 159 to 164 in c12bf20
This one, however, would not work for @JohelEGP case. Giving users the possibility to choose which interface is better for them might be a good thing here. |
If ImGui::DragInt("Frames", &frame_frequency.max.number_in<hertz>(), 1, frame_frequency.min.number_in<hertz>(), 1'000, "%d Hz"); |
Having an interface that, for the same single overload depending on some compile-time condition, returns either a reference or a value is a bad idea, in my opinion. |
I can see that breaking generic code. |
You can implement #412 (comment)'s:
IIUC, |
I recently had to interface my own hand rolled type wrapper (on a microcontroller) and had to call a c function like:
Thus such an interface is desired for a units library |
We can brainstorm how to extend the interface with additional "safer" overload sets. However, I think that the plain I am not sure if adding even more "safe" interfaces improves the experience here... For now, I would stay with |
That is fair. Now, |
An lvalue interface is surely desirable, but I think the question is whether it should include explicit units at the callsite. Your example above would work just as well with something like the following: quantity_point<celsius, float> t;
read_temperature(&t.stored_data_in(celsius)); The difference is that it has better callsite readability --- and, as @JohelEGP pointed out, is safer under future refactorings.
Did he? I didn't see the "I don't care about the unit" part. Rather, it seems to me that his use case does care about the unit!
This is the critical question: how to encourage the use of safer APIs? The most effective way I know is to make them the only APIs. 😄 This also has the virtuous effect of encouraging the APIs to be as concise and user-friendly as possible, to minimize user complaints. Indeed, I got several user complaints about my unit-safe interfaces. However, 100% of them occurred in the design proposal phase, before anyone actually used the interfaces. The years of production use haven't yielded a single complaint. |
Au has |
Sure, I agree. I am just having a hard time with the naming of such two overload sets. Right now, |
We could open a new issue, "How to rename the |
I'm using Dear ImGui.
It has interfaces like
ImGui::DragInt
:They are used like this:
frame_frequency.max
is ani32
object.I'd like that to be a quantity of hertz.
But
mp_units
doesn't give access to the number of a quantity, so I can't use it.To enable use of such interfaces, I propose making the underlying number accessible by the name of
unsafe_number
.So I propose reverting #412 and renaming
number
tounsafe_number
.I'm convinced that this is much safer and integrates better with existing code bases:
The text was updated successfully, but these errors were encountered: