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

Make Quantity::new const, or provide an alternative way to create constant quantities #251

Open
iliekturtles opened this issue Apr 14, 2021 · 3 comments

Comments

@iliekturtles
Copy link
Owner

See discussion starting at #26 (comment) and #250 (comment). Also see #165 for a general discussion about making as many functions as possible const.

@adamreichold
Copy link
Contributor

Personally, I think a new_with_base_units const fn does not provide a lot of benefit compared to a struct literal like

const FOO: Bar = Quantity {
  dimension: PhantomData,
  units: PhantomData,
  value: 23.0,
};

at least w.r.t. correctness of dimensions and units.

But for Quantity::new to become a const fn, the first order of business seems to be making to_base a const fn which would seem to require making Conversion useful in const context. However, I don't think this possible for heap-allocated storage types like BigInt.

Should we consider creating a duplicate machinery for ConstConversion, const_to_base etc. defined only for primitive integers and floating point numbers?

@iliekturtles
Copy link
Owner Author

I haven't really thought down the path of ConstConversion + Conversion. How would the two traits would? Would we have two different code paths for const/non-const?

As an aside I'm working on changes to to_base, from_base, and change_base to make them more stable an introduce less floating point imprecision.

@adamreichold
Copy link
Contributor

Would we have two different code paths for const/non-const?

I think this is necessary because a trait that requires a function/value to be available in const context cannot be implemented by heap-allocated types like BigUint. So we have to create separate code paths which apply only to those storage types which can be operated upon in const context which might even be limited to the primitive integers as const evaluation of floating point math is behind a separate feature gate ATM.

More concretely we would probably have something like

trait ConstConversion<V> {
  type T: ConstConversionFactor<V>;

  const COEFFICIENT: Self::T;
  const CONSTANT: Self::T;
}

and so on. ConstConversionFactor will also be interesting as we cannot use the standard traits Add, Mul, etc. as they do not specify const fn operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants