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

Allow constants to be defined in system! or quantity! macro #32

Open
iliekturtles opened this issue May 26, 2017 · 1 comment
Open

Allow constants to be defined in system! or quantity! macro #32

iliekturtles opened this issue May 26, 2017 · 1 comment

Comments

@iliekturtles
Copy link
Owner

Allow one location? Both?

Constant created by system macro (e.g. ISQ!) with the appropriate unit. Accessed in a submodule so there are no name collisions? let _ = uom::si::f32::velocity::c;

quantity! {
    quantity: Velocity; "velocity";
    dimension: ...;
    units { ... }
    constants {
        c: 299_792_458 meter_per_second,
    }
}

Constant created by system macro (e.g. ISQ!) with appropriate base units. let _ = uom::si::f32::G;

system! {
    quantities: ISQ {
        length: meter, L;
        ...
    }
    units: SI { ... }
    constants:{
        G: 6.674_083_1_E-11 ISQ<P3, N1, N2, Z0, Z0, Z0, Z0>,
    }
}
@iliekturtles
Copy link
Owner Author

Replying to @billyrieger in #126. I'd love to see a PR for this. If you want to start working through the open questions and put something together I'd be happy to provide assistance and review.

  • Constants are definitely in-scope. I think I would prefer to see them under the si module rather than as a separate crate unless there is a noticeable compilation time change. I also think that a feature may be overkill unless there is a compilation time change. I haven't fully thought this through so I'm open to any suggestions.
  • I'm not sure the best way to handle support for non-float underlying storage types either. I've been punting on this issue (see test failures with integers enabled #44) for a while now. Ideally the constants would actually be rust const by using <float literal> as V, but that makes it impossible to create constants for the non built-in types (bigint, ratio, ...). Generally num::FromPrimitive::from_f64 is used right now, but trait methods can't be const.
  • Also not sure the best location to place the constants. Using the quantity! macro? What about constants that belong to an unnamed quantity? Using the system! macro? Both? In si/mod.rs? In a constants module under si?
  • For constants to include I'd go through the NIST list and include the most common. Just including everything isn't a big deal either.
  • Use the constant symbol or the full constant name? Sort of leaning towards the full name.

Based on the limited time I spent thinking through the issue while replying below is one possible solution that is const and works for all built-in types. Usage would be uom::si::constants::f32::G.

pub mod constants {
    storage_types! {
        types: PrimInt, Float;
        /// Gravitational constant.
        pub const G: Quantity<uom::si::ISQ<P3, N1, N2, Z0, Z0, Z0, Z0>, uom::si::SI<f64>, f64> = Quantity { dimension: PhantomData, units: PhantomData, value: 6.674e-11 as V, };
    }
}

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

1 participant