-
Notifications
You must be signed in to change notification settings - Fork 85
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
Implicit construction of quantities from a value #410
Comments
Beginning of the thread: #391 (comment). The last point, "proposed solution", which is about sum types of quantities, feels very much like
|
Yes, that is a really good point! Another reason to consider such a change. |
It can't be done while disallowing |
Yes, I know. But the more I think about it, the more I am convinced that |
I tend to disagree here. To me, the main reason for using strongly typed engines is to avoid errors due to mis-interpretation between different units. These errors are of the worst kind, because they may be extremely hard to find. Because of that, I would prefer if the compiler would complain loudly whenever I separate units from values without having a clear and obvious marker (i.e. at the call site) that units are being converted. (For the same reason, I always prefer Is there a way we could make implicit conversions opt-in at the call-site?
This would probably require explicit specialisations for all classes of containers that should be supported, but to me, that would still be preferable. |
Done in V2. |
With the new design a following change was needed:
As @JohelEGP correctly noticed that:
Discussion of alternatives
A number and a unit are not enough to define a quantity. Even adding a dimension was found to be deficient. This was one of the biggest issues with the old "references".
Regarding function parameters, I am not the biggest fan of this syntax in general. Consider:
This looks really cryptic to me and might be a source of confusion. I would much prefer people to type:
So I do not think that there is a problem here. Additionally, to support such a scenario, we will have to introduce yet another type/abstraction that would handle such a scenario and make a quantity implicitly constructible from it. I am not so sure if it is worth it.
Another point here worth considering is that right now, we do the following to define a scaled unit:
and I hope that if expression aliases became a part of the C++ language, we will write:
so a
60 * s
is just yet another scaled unit and not a quantity value.Proposed solution
However, there is another even simpler solution to the problem. For now, the quantity cannot be implicitly constructible from the representation type, so we cannot just leave numbers to initialize the struct.
I believe that the creation of quantities from raw number arguments is quite rare and happens only on the boundaries of strongly typed engines. On the other hand, the initialization of some quantities from constant values will happen quite often, and sometimes, a quantity will be packed inside of the aggregate. Until now, we were not able to initialize such aggregates in an efficient way, and now with the V2 design it looks like this:
If we would make the constructor implicit, then the above could be rewritten as:
which I think could be a big improvement here.
On the other hand, it would allow something like:
which I am not so sure if it is a great idea, although I am ready to discuss and consider it.
Any ideas, thoughts, or comments are welcome here.
The text was updated successfully, but these errors were encountered: