Captain / Documentation / Part 3. The language

Integers, booleans and conversion

Contents ↓

This chapter covers Captain's simplest values: the integers i32 and i64, bool, and void; how they compare; and the one rule that runs through all of Captain's numbers, that a value never changes type unless the program says so. Decimals follow the same rules and have a part of their own: Exact decimals.

Integers

i32 and i64 are signed integers, 32 and 64 bits wide.

Literals and their type

An integer literal is an i32, unless its context expects an i64: a parameter, field or result declared i64, or the other operand of an operator that is an i64.

ints.capt
module Example::Ints.

export big -> i64 [
  ret 3000000000.
].

export later millis: i64 -> i64 [
  ret millis + 60000.
].

export wide n: i32 -> i64 [
  ret (n as: i64) * 3000000000.
].

A local takes the type of its first value, and a literal on its own is an i32. So a local meant to hold an i64 must start from one:

Refused by the checker: CAP-TYPE-010
module Example::Ints.

export start -> i64 [
  total := 0.
  ret total.
].

Write total := 0 as: i64., or start it from an i64 parameter.

A literal that does not fit its type is refused before the program runs. It is never wrapped or truncated:

Refused by the checker: CAP-TYPE-031
module Example::Ints.

export biggest -> i32 [
  ret 3000000000.
].

Arithmetic

+, -, * and / take two integers of the same type and give that type. Division truncates toward zero. There is no remainder operator and no unary minus: write 0 - n.

Overflow and division by zero are not wrapped. They stop the run with CAP-RUN-ARITH:

arith.capt
module Example::Arith.

export grow n: i32 -> i32 [
  ret n * 2.
].

export quotient a: i32 b: i32 -> i32 [
  ret a / b.
].
Terminal
$ captain build arith.capt -o arith.capb
built arith.capb ([..] bytes, sha256 [..])
$ captain run arith.capb grow n=1000000000
run log: grow.runlog
2000000000
$ captain run arith.capb grow n=2000000000
run log: grow.runlog
error[CAP-RUN-ARITH]: arithmetic failure (overflow or /0)
$ captain run arith.capb quotient a=-7 b=2
run log: quotient.runlog
-3
$ captain run arith.capb quotient a=7 b=0
run log: quotient.runlog
error[CAP-RUN-ARITH]: arithmetic failure (overflow or /0)

A wrapped integer is a wrong answer that looks right. A stopped run is a wrong answer that says so, and the run record shows where.

Booleans

bool has two values, true and false. It is not a number, and no other type stands in for it: there is no "truthy" integer or empty text.

Written Gives
a & b, a and: b true when both are; b is evaluated only when a is true
a \| b, a or: b true when either is; b is evaluated only when a is false
(b not) the opposite
a = b, a != b equality

and: and or: are the same operators, spelled as Smalltalk spells them. Their argument is an expression, not a block, and it is already evaluated only when needed.

& binds tighter than |, and both bind looser than comparisons, so (low <= x) & (x <= high) can also be written low <= x & x <= high.

bools.capt
module Example::Bools.

export within low: i32 x: i32 high: i32 -> bool [
  ret (low <= x) & (x <= high).
].

export outside low: i32 x: i32 high: i32 -> bool [
  ret ((within low: low x: x high: high) not).
].

An integer where a bool is needed is a type error:

Refused by the checker: CAP-TYPE-010
module Example::Bools.

export isSet flags: i32 -> bool [
  ret flags.
].

ifTrue:, ifFalse: and whileTrue: are methods of bool, not of integers. Blocks and control covers them.

void

void is the type of "no value". It is what a procedure with no -> returns, and what ifTrue:, forEach: and the other control forms give. It cannot be bound to a name, passed or returned.

Comparison

<, <=, > and >= compare two values of one ordered type: two integers of the same type, two decimals, two quantities of one dimension, or two texts (which order by code point). = and != also apply to bool, symbols, octets, and tagged values of the same type.

Both sides must have the same type. An i32 is not compared with an i64:

Refused by the checker: CAP-TYPE-011
module Example::Compare.

export fits n: i32 limit: i64 -> bool [
  ret n < limit.
].

Write (n as: i64) < limit.

Comparisons do not chain. a < b < c is refused with CAP-PARSE-CMPCHAIN; write (a < b) & (b < c).

Structs, enums and lists have no =: comparing two of them is CAP-TYPE-011, with the help to compare their fields or elements. What equality means for a record depends on the program, so the program says which fields it compares.

No implicit conversion

Captain never converts a value from one type to another on its own. Mixing an i32 and an i64, or an integer and a decimal, in one operation is refused:

Refused by the checker: CAP-TYPE-015
module Example::Mix.

export total count: i32 base: i64 -> i64 [
  ret base + count.
].

Every conversion is written, so a reviewer can find each one, and none of them hides a loss. There are three kinds, and each says what it can do.

as: converts a number exactly

n as: T converts a number to a type that holds every value it could have:

From To
i32 i64
i32 or i64 Decimal, at scale 0
any type itself

Because each of these is exact, as: never fails at run time. A conversion that could lose something does not exist, and asking for one is refused with CAP-TYPE-033:

Refused by the checker: CAP-TYPE-033
module Example::Narrow.

export narrow total: i64 -> i32 [
  ret total as: i32.
].

The help line says what to do: keep it an i64. A decimal does not become an integer; round it to scale 0 with x scale: 0 rounding: #Down and keep it a Decimal (see Division and rounding).

parse: reads a value from text

Text from outside may not be a number at all, so reading one gives a Result: Int parse: t gives Result[i32], ignoring white space around the digits. Each tagged type has its own parse: (Symbols, quantities and tagged values). as: on text is refused, with a help line pointing at parse:.

Text from: formats a value

Text from: x makes text of an integer, decimal, text, symbol, quantity or tagged value. It does not apply to bool, lists or records. Their text form depends on the program, so the program builds it.

convert.capt
module Example::Convert.

export count t: Text -> Result[i32] [
  ret Int parse: t.
].

export describe n: i32 -> Text [
  ret "you have " append: (Text from: n).
].

export asDecimal n: i32 -> Decimal [
  ret n as: Decimal.
].
Terminal
$ captain build convert.capt -o convert.capb
built convert.capb ([..] bytes, sha256 [..])
$ captain run convert.capb count t=" 42 "
run log: count.runlog
Ok { v: 42 }
$ captain run convert.capb count t=4.2
run log: count.runlog
Fail { err: Err { kind: #IntParseError, message: "`4.2` is not an i32" } }
$ captain run convert.capb describe n=3
run log: describe.runlog
"you have 3"
$ captain run convert.capb asDecimal n=7
run log: asDecimal.runlog
7

The three spellings come from Zing, the language Captain is a dialect of: as: for conversions that cannot fail, parse: for reading text that might not be valid, and Text from: for formatting. A reader who sees one knows whether it can fail and what it can lose.

Conversions in the standard library lists them all.