Skip to main content

dim

Macro dim 

Source
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 to Qty<D> via .as_qty().
  • Integer literals become Dimensionless::constant(n).as_qty().
  • +, -, *, / use the Qty operator impls which track dimensions at the type level.
  • x^n for small integer n (0–8) expands to repeated multiplication, preserving type-level dimension tracking. For larger or non-literal exponents, the macro falls back to extracting the inner Ex and using .powi() / .pow(), which loses dimension tracking (treats result as dimensionless).
  • Functions like sin, cos, exp, ln extract the inner Ex, call the method, and wrap the result as Dimensionless.

§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);