unit - pannous/warp GitHub Wiki

as in julia and math, a number next to an object means multiplication:

# x stands for any variable
3x == 3*x

we have a problem with significant whitespace though:

# x stands for any variable
3 x == [3 x]

as defined by commaless list semantics

so spaced, 3 x stays the list; only a unit multiplies when spaced (5 m is 5m, with a hint to write 5m), decisions #3.

since the first case is unambivalent it works out of the box and is especially useful for units:

3km+10m == 3010 meters

Runs today (warp)

A quantity is a number with a unit, written glued (3km) or spaced with the short or long name (3010 meters). A unit after an expression belongs to its last amount, so it binds tighter than a comparison; in converts:

3km + 10m == 3010 meters
90 minutes in hours

A quantity prints in the unit it was written in: print 2 h; print 120 min prints 2h and 120min; in changes it.

A variable or parameter named like a unit is the variable: meters = 5; meters + 1 is 6, s => s + 1 takes s.

Temperatures are units counted from a zero of their own: °C, °F and K (Kelvin). A conversion and a comparison read each amount from its zero:

40°C as °F

gives 104°F, and 0°C == 32°F is true. Temperatures add only in one unit: 20°C + 5°C is 25°C, while 20°C + 50°F is an error that asks to convert one first (50°F as °C). They compute at compile time only for now: in a loop or a function a temperature is a loud error.

In variables, loops and functions

Quantities compute at run time too; the compiler checks their units, so a mismatch is an error before the program runs. A unit is written on its number (5m); 5 m works with a hint.

speed(d, t) := d/t
speed(10km, 2h)       # 5km/h
total = 0m
for i in 1..3 { total += 5m }
total                 # 10m

as picks the unit shown (2500m as km is 2.5km), "${d}" shows the unit, lists and object fields keep theirs (p = {dist: 0m}; p.dist += 250m). 3m + 2s is a DimensionError.

Units known only at run time, such as input text, go through quantity:

q = quantity("5 km") + quantity("500 m")
q.in("m")             # 5500

x as quantity (or x as unit) is quantity(x): input as quantity.

Angles

° is π/180, exact: 30° == π/6, sin(30°) is 0.5, and sin(x°) takes a variable in degrees.