dim!() { /* proc-macro */ }Expand description
Build a dimension-checked physical quantity using natural math syntax.
§Syntax
ⓘ
dim!(OutputType: math_expression)The macro parses the math expression (same syntax as expr!), generates
code that operates on Qty<D> values (preserving compile-time dimension
tracking), and converts the result to OutputType via FromDimExpr.
If the computed dimension doesn’t match OutputType, the compiler emits
a clear error message.
§How it works
- Identifiers refer to named quantity variables (e.g.
Mass,Length). They are cloned and converted toQty<D>via.as_qty(). - Integer literals become
Dimensionless::constant(n).as_qty(). +,-,*,/use theQtyoperator impls which track dimensions at the type level.x^nfor small integern(0–8) expands to repeated multiplication, preserving type-level dimension tracking. For larger or non-literal exponents, the macro falls back to extracting the innerExand using.powi()/.pow(), which loses dimension tracking (treats result as dimensionless).- Functions like
sin,cos,exp,lnextract the innerEx, call the method, and wrap the result asDimensionless.
§Examples
ⓘ
use symplex::prelude::*;
use symplex::units::*;
let m = Mass::symbol("m");
let g = Acceleration::symbol("g");
let h = Length::symbol("h");
let pe = symplex::dim!(Energy: m * g * h);