This file is the main user facing APIThe main concept used for type matching of unitsCTCompoundUnit logicGeneral user facing APIThis is a forced conversion of a unit to a FloatType. It simply removes any unit from the type. Currently just x.FloatType. Might change in the future.auto conversion of UnitLess to FloatType is possible so that e.g. sin(5.kg / 10.kg) works as expected!Pretty printing of unitsTODO: we should really combine these macros somewhat?Natural unit stuff
Procs
proc prettyImpl(s: FloatType; typStr: string; precision: int; short: bool; format: FloatFormatMode): string {....raises: [], tags: [], forbids: [].}
- Source Edit
Converters
converter toRawFloat(x: UnitLess): FloatType {....raises: [], tags: [], forbids: [].}
- Source Edit
converter toUnitLess(x: float32): UnitLess {....raises: [], tags: [], forbids: [].}
- Source Edit
converter toUnitLess(x: float64): UnitLess {....raises: [], tags: [], forbids: [].}
- Source Edit
converter toUnitLess(x: SomeNumber): UnitLess
- Source Edit
Macros
macro defUnit(arg: untyped; toExport: bool = false): untyped
-
Defines the given unit arg the scope. If toExport is true and the call happens at top level, the unit will be exported.
This is required for any not predefined compound unit (product of different units), unless the same unit appeared "naturally" due to a math operation or . dot operator assigning a unit. Note however that it may still be desired to call defUnit manually to be certain the unit is available, as implicitly defined units are only visible in the scope 'below' their definition (which can lead to confusing errors).
Source Edit macro quantityOf[T: SomeUnit](x: T): untyped
-
Returns a string representation of the full quantity of the given unit in base quantities.
Very useful for manual understanding / dimensional analysis.
Source Edit macro toBaseUnits[T: SomeUnit](x: T): untyped
-
Converts the given input unit to the fully flattened base type.
This is mostly a convenient tool if one wishes to quickly check the pure base unit representation of a unit.
Source Edit macro toDef[T: SomeUnit](x: T; to: untyped): untyped
-
A macro version of to above, which works for target types to, which have not been defined via defUnit yet. Calls defUnit and then dispatches to to.
NOTE: Under certain use cases the order of evaluation by the Nim compiler can lead to "type mismatch" errors. For example
block: defUnit(km•h⁻¹) proc foo[M: Mass; A: Acceleration](m: M, a: A): km•h⁻¹ = result = (m * a).toDef(km•h⁻¹)
in this case the compiler won't understand that km•h⁻¹ is already defined leading to ambiguous errors ("got X, expected X = Alias").
Therefore, use at your own risk. Useful in short pieces of code though!
Source Edit macro toNaturalUnit[T: SomeUnit](t: T): untyped
- parses the unit and converts it to natural units (eV) according to the contained Source Edit
macro unitOf[T: SomeUnit](s: T): untyped
-
A helper to only get the unit name of a type as a string.
It does not replace typeof, but typeof is not very helpful if one is interested in the name of a unit type only, because of the way the compiler treats aliases. Hence, for string names of a unit, use this.
Note: behavior of this may change in the future and may currently give slightly different representations than a regular $ call.
Source Edit