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

q[m / s] or q.in(m / s) #469

Closed
mpusz opened this issue Jun 24, 2023 · 5 comments
Closed

q[m / s] or q.in(m / s) #469

mpusz opened this issue Jun 24, 2023 · 5 comments
Labels
design Design-related discussion question Further information is requested

Comments

@mpusz
Copy link
Owner

mpusz commented Jun 24, 2023

First, I would like to state that I personally love the syntax that creates a reference based on quantity_spec and Unit:

quantity<isq::height[m]> q;

I wouldn't like to make the above gone.

However, yesterday I wrote the following example in our documentation:

auto q1 = 42 * W;
std::cout << q1 << "\n";
std::cout << q1[J / s] << "\n";
std::cout << q1[N * m / s] << "\n";
std::cout << q1[kg * m2 / s3] << "\n";

Initially, I provided this operator to entertain myself with this idea and for consistency with the above. However, there are a few significant differences here:

  1. isq::height[m] is a pure compile-time operation with no runtime overhead. It also never touches the quantity value.
  2. q[m], of course, creates a new type in compile-time but also involves a runtime value rescaling operation according to the provided new unit. It can also lead to overflows or losing the precision of a quantity value.

Even more, we now have a q.number_in(si::metre) (#412), which returns a raw value in a provided unit.

So here comes the questions:

  1. Should we add q.in(si:::metre) to express runtime behavior and for consistency with number_in()?
  2. Should we remove the q[si::metre]?
  3. Should we have both solutions?
@mpusz mpusz added design Design-related discussion question Further information is requested labels Jun 24, 2023
@JohelEGP
Copy link
Collaborator

  1. Should we have both solutions?

q.in(si::metre).number() can replace q.number_in(si::metre).

@mpusz
Copy link
Owner Author

mpusz commented Jun 24, 2023

Well, we can also use q[si::metre].number() now.

@mpusz
Copy link
Owner Author

mpusz commented Jun 24, 2023

BTW, there is one more important difference in behavior between isq::height[m] and q[m]. The first one is provided unconditionally, while the second one is available only for non-truncating conversion. Otherwise value_cast<m>(q) needs to be used.

@JohelEGP
Copy link
Collaborator

  1. Should we have both solutions?

q.in(si::metre).number() can replace q.number_in(si::metre).

IIRC, the proponents of number_in suggested removing number because it's unsafe.
So to get the number you have to specify the unit.

@mpusz
Copy link
Owner Author

mpusz commented Jun 24, 2023

On the other hand, q.number_in(m), as of now, has the same constraints as q[m].

@mpusz mpusz closed this as completed in ae92b49 Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design-related discussion question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants