6. Types
Wax's types are Unio's, with Unio's rules for them (D16). Unio's standard, chapter 7, has them in full; read its examples in Wax's syntax (README).
| Type | What it holds | At the doors, in Rust |
|---|---|---|
Whole | a 64-bit signed whole number, stopping with a failed check on overflow (Unio D15, D46) | i64 |
HugeWhole | a whole number of any size, limited only by the script's memory cap (D14, D115) | a big integer, the building session's choice |
Decimal | an IEEE 754 decimal128 number: 34 decimal digits, exact for numbers written with that many, rounding past them (D116) | the building session's choice |
HugeDecimal | a decimal number of any size, exact (D116) | the building session's choice |
Float | an IEEE 754 binary64 number, with ~ on inexact literals and Unio's rules for showing, rounding and comparing (Unio D155, D157, D175 to D184) | f64 |
Bool | true or false | bool |
Text | text | String |
List<T> | a list of Ts, with an optional most it may hold, len.max (Unio D167) | Vec |
Array<T>[len: n] | exactly n Ts (Unio D167) | Vec |
- A number never starts with a leading zero, as
007;0itself and0.5are fine (D93). - A number with a point is a
Float, and one without is aWhole(Unio D180), so a worked-out type is never in doubt for a literal:speed = 2.0makes aFloat. WholeandFloatdon't mix quietly:be.Float(lives) * speed(Unio D162).- A
Wholebecomes aHugeWholeby itself, since it always fits exactly, as incookies + clicks; aHugeWholebecomes aWholeonly throughbe.Whole, failing withtoo_bigwhen it doesn't fit, and aHugeWholeand aFloatdon't mix quietly (D115). A number written too big for aWholeis aHugeWholewhere one is wanted, and refused elsewhere (D115). - A
Decimalcounts in tens, so0.1is exactly 0.1. It holds 34 digits; past them it rounds, nearest with ties to even, and/rounds the same way. It never shows~, since what it shows is always exactly what it holds (D124). It keeps its trailing zeros as IEEE decimal128 does, so1.50 + 1.50shows3.00;eqcompares amounts, so3.00 eq 3.0(D125). AHugeDecimalnever rounds on+,-or*, and its/gives the exact answer or stops, as/on aWholedoes (D116, Unio D70, D155). - Where it always fits, a number becomes the bigger type by itself: a
Wholeinto aDecimalorHugeDecimal, aHugeWholeinto aHugeDecimal, and aDecimalinto aHugeDecimal. Every other way is written withbe.and can fail, and aFloatmixes quietly with none of them (D116). - A number written with a point is a
DecimalorHugeDecimalwhere one is wanted, exactly as written, the other side of an operator included, sogold + 0.5andgold > 0.5just work withgoldaDecimal; likewise a whole number too big for aWholebeside aHugeWhole(D116, D119). - Text counts graphemes by default, what a person sees as one character:
.lencounts them, and[pos:],[offset:]andfor_eacheach give one grapheme as aText..len.ucpcounts Unicode code points and.len.bytesUTF-8 bytes. Each Wax version pins one Unicode version (D65, D66). .split(by: t)gives aList<Text>, keeping empty pieces, and.split.no_empty(by: t)drops them (D65).- Splitting by empty text is refused while loading wherever the empty text can be seen, as
split(by: ""); one that only turns out empty while running is a failed check, as dividing by zero is (D89). eqandnot_eqcompare two texts exactly, character by character, with nothing normalised:"Yes" not_eq "yes";<and>on text are refused for now, since text has no one right order (D64, D96).eqandnot_eqcompare bigger values by their contents (D95): records field by field; lists and arrays by length and their items in order;Dicts by their keys, each with an equal value, whatever order they were added in; andone_ofs by their case and its values.- Text is written in double quotes, and joined only by filling it in with braces, as in
"{first} {last}"(D16, D75, D77, Unio D21, D201). Wax has no operator for joining text:&is refused, and so is+on text, each with the braces form as the fix. - Maths:
+,-,*,/,div.floor,div.trunc,remandsqrt(D22, Unio D27, D70, D71, D185). Floats are checked after every operation (D18). **raises to a power, binding tighter than*, so2 * x ** 2is2 * (x ** 2)(D108). It follows maths order: a chain groups from the right,2 ** 3 ** 2being2 ** (3 ** 2), and it comes before a negating-, so-2 ** 2is-(2 ** 2), and(-2) ** 2is 4 (D132).Whole ** Wholegives aWhole, stopping with the overflow check when it's too big, and a negative power of aWholeis refused;Float ** Floatgives aFloat, correctly rounded (D50). The two don't mix.- Lists:
[ ]literals,pos:andoffset:,.add,.remove,.len,len.maxwith its failurefull, and[all: value](D22, Unio D75 to D80, D167 to D169). - A list's
len.maxis part of its type: moving a list into a limit at least as big, or none, is allowed, and into a smaller one it can fail withfull, asbig_bag ?? []; a literal longer than itslen.maxis refused (D73).
mut bag: List<Text>[len.max: 10] = []
board: Array<Whole>[len: 64] = [all: 0]
A name's type can be written after it, as name: Type = value. It's optional, and required only when the value can't
say the type, as with an empty list: mut bag = [] is refused (D30).
A written type can leave out what the value shows and write only the rest, such as a length the value can't give:
board: Array[len: 64] = [all: 0] takes Whole from the 0. What the value doesn't show must still be written, as
the item type of an empty list: bag: List[len.max: 10] = [] is refused (D82).
A type's setting, as len.max: or len:, is a whole number written there, or a name made at the top level from a
whole number written there and never changed, with no mut, as max_items = 10 and then
bag: List<Text>[len.max: max_items] = []. Any other name, a calculation or a call is refused, since its value isn't
known while loading (D103).
Units
Units are optional: a Whole or a Float can carry one, as Cyborg's do, and nothing is ever required to (D111):
wait(2 secs)
wait(500 ms)
total = 2 secs + 500 ms // 2500 ms
- A number takes its unit bare, right after it, as
2 secs. Units belong to groups, and the group is written, astime::secs, only to settle a clash between two groups with a unit of the same name. A unit only ever stands after a number or in a type, so it never takes a word away from names (D111, D112). - Units of one group combine and convert by themselves when the result is exact:
2 secs + 500 msis2500 ms, in the finer unit, so aWholestays exact, and a value goes where another unit of its group is wanted when it converts exactly. A conversion that can't be exact is refused, with the fix saying how to round. Units of different groups, as seconds and pixels, never combine by+or-(D120). perwrites a rate right where it's used, as120.0 px per secs, with no declaration: multiplying and dividing work the units out, sospeed * dtwithdtinsecsgivespx, and a result is refused where its units don't fit (D113, D120).- Where a unit is expected, a plain number is refused, with the unit added as the fix, so a door taking a time
can't be given a bare
2(D111). - Units have short names, as
secsandms, since their group says what they measure (D111). tomeasures a value in a unit, giving the plain number it reads as there:total to secsis2.5andtotal to msis2500fortotalof2500 ms(D148, replacing D122'sin, which stays the loop's). Maths beforetoneeds brackets, as(a + b) to ms;a + b to msis refused, with the brackets as the fix (D149). The value is unchanged; only a unit of its own group can measure it, and the result is aFloatwhere it can't be a whole number (D122).- A unit is an amount, a span, unless it's marked a point, with the narrowing
.point, aspx.pointfor a place on screen orsecs.pointfor a moment. A point plus a span is a point, a point minus a point is a span, and two points added are refused (D121). - Prefixes and showing a unit follow Cyborg's units where Wax hasn't decided its own (D111).
Wax's own unit groups are (D113):
| Group | Units |
|---|---|
time | ms, secs, mins, frames |
angle | degs, rads, turns |
distance | px, tiles |
frames and tiles have no fixed size, so they convert only through a rate, as 16.0 px per tiles, or a frame's
measured time in secs per frames. Other units, as money or a game's own, a script or host declares as a group of
its own (D113).
A script declares a unit group of its own with group.unit, its members written _::name (D179), one of them marked
base, and each other one defined as so many of an earlier unit (D123):
group.unit money
_::cents base
_::dollars = 100 cents
end
price = 3 dollars + 50 cents // 350 cents
An inexact ratio is written with ~, as a Float is.
Ranged numbers
A Whole or a Float can be given a lowest and a highest value as type settings (D107):
mut gold: Whole[min: 0] = 0
mut hp: Whole[min.clamp: 0, max.clamp: 100] = 100
mut speed: Float[min: 0.0, max: 12.5] = 0.0
min:andmax:are hard limits: storing a value outside them stops the script with a failed check, and one Wax can see while loading, asgold: Whole[min: 0] = -5, is refused (D18, D54, D107).min.clamp:andmax.clamp:pull a value back to that edge each time one is stored, sohp = hp - 150leaveshpat 0. Each edge chooses for itself (D107).- A ranged number is its type plus a check: a plain
Wholegoes into aWhole[min: 0]freely, checked or pulled back as it lands, and comes out with nothing to do. It lands wherever a value is stored: a name, a field, a list item, a parameter, and areturn(D128). - Either can be left off, and both are type settings, so each is a number written there or a top-level name made from one (D103).
For edges only known while the script runs, clamp does the same to one value (chapter 8).
A type with more than one blank to fill in labels each one, as Dict<key: Text, value: Whole>; one with a single
blank, as List<Text>, doesn't (D31).
Not decided: other types.
Records
record Enemy
name: Text
hp: Whole
x: Float
y: Float
end
mut goblin = Enemy[name: "Goblin", hp: 30, x: 0.0, y: 0.0]
goblin.hp = goblin.hp - 5
record Namedeclares a record, its fields one per line, each with its type, closed byend(D20, D27).It's built with
[ ], naming every field; a field left out is refused (D27).A field is read with
.field, and changed only through amutname. A record is a value:b = agivesbits own copy (D15, D27).A change can reach through any depth of fields and positions of a
mutname, asbodies[pos: 1].vx = 2.0, with a method allowed at the end, asgoblin.items.add("Gem"); aDictkey partway along is refused, since a missing key would have nowhere to be dealt with: the value is taken out, changed and put back (D62, D90).At the doors a record is a Rust struct (D27).
A record can hold a record, and a type can hold itself where a value can end, through a
one_ofcase, aList, aDictor aMaybe; with no way to end, as a fieldnext: Nodeinrecord Node, it's refused (D72).
Methods
A record or a one_of can have methods: procs written inside its block, after its fields or cases, each taking a
value of the type as its first parameter, and called only with a dot on such a value (D163, D164):
record Enemy
name: Text
hp: Whole
proc is_alive[enemy: Enemy] -> Bool
return enemy.hp > 0
end
end
goblin = Enemy[name: "Goblin", hp: 30]
alive = goblin.is_alive()
- The value before the dot is the first parameter, and the rest are given in
( )as any call's are. - Two types can each have a method of the same name, as
sizeonEnemyand onTree, since a method is only ever reached through a value of its type. - A method that changes its value marks its first parameter
mut, asproc take_damage[mut enemy: Enemy, amount: Whole]. Inside, it's a mutable variable; when the method ends, its value goes back into the variable it was called on, which must be mutable, as forbag.add(D165, Unio D77). A top-level variable can't be lent this way to a method that also reaches it directly (D165, as D69).
Not decided: defaults for fields; methods on types a script didn't declare; mut parameters outside methods.
Dictionaries
mut stock: Dict<key: Text, value: Whole> = ["sword": 1, "potion": 3]
stock[key: "potion"] = 4
potions = stock[key: "potion"] ?? 0
Dict<key: Key, value: Value>holds values looked up by key, each key once.Dictis short for dictionary (D32).- Its literal writes the keys as values, and
[:]is an empty one; the same key twice in one literal is refused (D32, D73). - It's reached with
key:; setting a key through amutname replaces its value or adds it (D32). - A missing key is the failure
missing, handled with??orotherwise(D32, Unio D23). - It keeps the order keys were added in, and is copied as a whole, a Rust
HashMapat the doors (D15, D32).
for_each goes through a Dict's values in the order their keys were added, and at key name gives each key (D33):
for_each count at key item in stock
show_msg("{item}: {count}")
end
.remove(key: k)takes a key out, failing withmissingif it isn't there;.has(key: k)gives aBool;.lenis the number of keys (D34).- Keys can be
Whole,TextorBool; aFloatkey is refused (D34).
Not decided: lists and records as keys; other methods.
Options
one_of Event
idle
hurt[amount: Whole]
healed[amount: Whole, by: Text]
end
mut last = Event.hurt[amount: 5]
one_ofdeclares a type whose value is exactly one of its cases, listed one per line, closed withend; one with no cases is refused, since no value of it could exist (D35, D36, D93).- A case may carry values, named in
[ ], built as a record is (D27, D35). - A case is written as a narrowing of its type, as
Event.hurt[amount: 5]andState.idle; one the type doesn't have is refused at load time (D35, Unio D41).
match reads which case a value is (D37):
match last
case hurt[amount]
hp = hp - amount
case healed[by]
show_msg("{by} healed you")
case idle
end
- Every case must be covered; there's no catch-all (D37).
- A case's values come out by name, read-only, for that case's body, and a case takes out only the values it reads, in their declared order (D37, D61, D87).
- A case runs until the next
case, and oneendcloses thematch(D37, D102).
Maybe
Wax has no nil, null or None: a name of a type always holds a real value of that type (D38). "Might not be
there" is Maybe<T>, a built-in one_of with the cases none and some[value: T], read with match or given a
fallback with ??, and Rust's Option<T> at the doors (D38):
target = nearest_enemy()
match target
case some[value]
attack(value)
case none
end
id = nearest_enemy() ?? 0
A Maybe is made as any one_of's case is (D35), as Maybe.some[value: p] or Maybe.none. Like [], a
Maybe.none can't say its T, so its type is written where nothing else says it, as found: Maybe<Whole> = Maybe.none (D30); a return into a procedure's written result type already says it (D81).
?? crash on a Maybe stops the script with a failed check when it's none, as for a failure (D54, D79). ?? ignore
on a Maybe is refused, since the name would be left with nothing in it, with ?? and a value as the fix (D79).
Filling in text
Every text fills in what's in its braces, wherever it's written (D75, Unio D200):
mut msg = "Hello, {player}"
show_msg("Correct! You got it in {tries} tries")
- Braces hold a name, which may be followed by its fields and facts, as
{goblin.name}and{names.len}, never a calculation or a call; a name, field or fact that doesn't exist is refused at load time (D12, D28, Unio D29). - Every value shows as it's written in Wax, a list, record,
Dictorone_ofincluded, with text inside in its quotes and aFloatwith its~when it isn't exact:"{bag}"with["Gem", "Key"]shows["Gem", "Key"], andbe.Textgives the same (D100, Unio D155). {newln}and{tab}give a line break and a tab, built-in names filled in like any other, as"Game over{newln}Score: {score}"; no name can benewlnortab(D71, D118). They exist only in braces, so the text holding just a line break is"{newln}", as insave.split(by: "{newln}")(D129).- Empty braces, as
"{}", are refused, with"\{}"as the fix (D93). - A brace to show as it is is written
\{, as in"Type \{help}", which showsType {help}(D63, D75, Unio D30).
A Float in braces can take places: after a comma, showing exactly that many digits after the point, rounded to the
nearest with a tie going away from zero, and with no ~, as Unio's chapter 7 has it (D94, Unio D175, D176, D178):
show_msg("Speed: {speed, places: 2}") // Speed: 0.30
Converting
be.Whole, be.Float and be.Text, with be.Whole.floor, .trunc, .ceil, .nearest and .nearest.even for a
Float, as in Unio (D22, Unio D22, D153, D178). Unio's standard, chapter 7, has them in full.
be.Boolreads"true"or"false", as aBoolshows, and fails withnot_a_boolon any other text (D101).be.Texttakes any value, shown as it's written (D100).The new number types take these operations (D127):
HugeWholeDecimalHugeDecimal+,-,*, comparisonsyes yes yes, exact /exact or stops, as on a Wholerounds to 34 digits exact or stops div.floor,div.trunc,remas on a Wholea whole-valued Decimaland what's leftthe same, exact **whole powers whole powers, rounding past 34 digits whole powers, exact abs,min,max,clampyes yes yes sqrtno correctly rounded exact or stops lerpno yes no the angle built-ins no no no Between the number types,
be.converts every way that isn't automatic (D126): into aWholeorHugeWholefrom a decimal, saying how it rounds, asbe.Whole.floor(price), as from aFloat, failing withtoo_bigwhen it doesn't fit, asbe.Whole(huge)does; into aDecimalfrom aHugeDecimal, rounding to 34 digits; into aFloatfrom a decimal, to the nearestFloat; and into aDecimalfrom aFloatwithbe.Decimal.shown, the number theFloatshows, orbe.Decimal.nearest, the nearest to what it holds, a barebe.Decimalon aFloatrefused.be.HugeWhole,be.Decimalandbe.HugeDecimalread text asbe.Wholedoes, failing withnot_a_number.Any other conversion is refused, as text into a list, or a
Wholeinto aBool(D101).