New units of measurement can be introduced with the
unit keyword. There are two types of units: base units and derived units.
A new base unit can be defined by specifying the physical dimension it represents. For example, in the International System of Units (SI), the second is the base unit for measuring times:
unit second: Time
Time denotes the physical dimension. To learn more, you can read the corresponding chapter. But for now, we can just assume that they are already given.
Derived units are also introduced with the
unit keyword. But unlike base units, they are defined through their relation to
other units. For example, a minute can be defined as
unit minute: Time = 60 second
: Time annotation is optional. If a dimension is specified, it will be used to verify that the right hand side expression (
60 second) is indeed of physical dimension
Time. This is apparent in this simple example, but can be useful for more complicated unit definitions like
unit farad: Capacitance = ampere^2 second^4 / (kilogram meter^2)
If a unit may be used with metric prefixes such as
M, we can prepend the unit definition with the
@metric_prefixes unit second: Time
This allows identifiers such as
millisecond to be used in calculations. See the section below how prefixes interact with aliases.
Similarly, if a unit should be prependable with binary (IEC) prefixes such as
Gi, you can
@binary_prefixes decorator. A unit might also allow for both metric and binary prefixes, for example:
@binary_prefixes @metric_prefixes unit byte = 8 bit
This allows the usage of both
mebibyte (1024² byte) as well as
megabyte (1000² byte).
It is often useful to define alternative names for a unit. For example, we might want to use the plural form
seconds or the commonly
used short version
s. We can use the
@aliases decorator to specify them:
@metric_prefixes @aliases(meters, metre, metres, m: short) unit meter: Length
In addition to the name, we can also specify how aliases interact with prefixes using
: long (the default),
: both or
: none. The actual unit name (
meter) and all
long aliases will accept the long version of prefixes (…,
short aliases (
m in the example above) will only accept the respective short versions of the prefixes (…,
Aliases annotated with
: both or
: none accept either both long and short prefixes, or none of them.
The unit definition above allows all of following expressions:
millimeter kilometer millimeters kilometers millimetre kilometre millimetres kilometres mm km ...