src/unchained/units

Search:
Group by:
Source   Edit  

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

Types

SomeUnit = concept x
    isAUnit(x)
Source   Edit  

Procs

proc `*=`[T: SomeUnit](x: var T; y: UnitLess)
Source   Edit  
proc `+=`[T: SomeUnit](x: var T; y: T)
Source   Edit  
proc `-`[T: SomeUnit](x: T): T
Source   Edit  
proc `-=`[T: SomeUnit](x: var T; y: T)
Source   Edit  
proc `/=`[T: SomeUnit](x: var T; y: UnitLess)
Source   Edit  
proc abs[T: SomeUnit](t: T): T
Source   Edit  
proc hash[T: SomeUnit](x: T): Hash
Source   Edit  
proc prettyImpl(s: FloatType; typStr: string; precision: int; short: bool;
                format: FloatFormatMode): string {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc toFloat[T: SomeUnit](x: T): FloatType
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 `$`[T: SomeUnit](s: T): string
Source   Edit  
macro `*`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
TODO: can we extract the actual mathy part from x, y instead of using the whole expression? And then reinsert that after our change Source   Edit  
macro `+`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
Source   Edit  
macro `-`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
Source   Edit  
macro `.`[T: SomeUnit | SomeNumber](x: T; y: untyped): untyped
macro to allow to generate new types on the fly TODO: maybe have an explicit distinction between already defined types and not defined types? Use .! or something like this instead? Source   Edit  
macro `/`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
Source   Edit  
macro `<`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
Source   Edit  
macro `<=`[T: SomeUnit | SomeNumber; U: SomeUnit | SomeNumber](x: T; y: U): untyped
Source   Edit  
macro `==`[T: SomeUnit; U: SomeUnit](x: T; y: U): bool
Source   Edit  
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 pretty[T: SomeUnit](s: T; precision: int; short: bool; format = ffDefault): untyped
Equivalent to $, but allows to change precision and switch to long format. 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 sqrt[T: SomeUnit](t: T): untyped

Implements the sqrt of a given unitful value.

Fails if the given unit is not a perfect square (i.e. each compound of the full unit's power is a multiple of 2).

Source   Edit  
macro to[T: SomeUnit; U: SomeUnit](x: T; to: typedesc[U]): U
Converts the given unit x to the desired target unit to. The units must represent the same quantities, otherwise a CT error is thrown. 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