Variables and types
Variables
Section titled “Variables”let name = "Ada" # cannot changevar count = 0 # can changecount += 1let limit: int = 10 # an optional annotationprint "{name} {count} {limit}"Every value must be initialized. Declaring the same name twice in one scope is an error. Local types are inferred, so annotations are optional.
A task cannot read or write a top-level var: pass it as a parameter. A task may read a top-level let whose value is a literal.
Basic types
Section titled “Basic types”| Type | Meaning |
|---|---|
int |
64-bit signed integer |
float |
64-bit IEEE float |
bool |
true or false |
text |
Immutable UTF-8 string |
byte |
8-bit unsigned integer |
i8 i16 i32 u16 u32 u64 f32 |
Sized numbers |
list[T], map[K, V], set[T] |
Collections, see Collections |
none |
The absence of a value |
Numbers
Section titled “Numbers”There are no implicit conversions. Each number type has a task for every other one, named after its target:
let n = 7let x = n.to_float() / 2.0let small = 200.to_u8()print x # 3.5print small # 200print 2.0.sqrt()/always gives a float type, so7 / 2is3.5.//divides integers and rounds down, and%takes the sign of the divisor.- Integer overflow traps in every build. For wrapping math, call it out:
a.wrapping_add(b),wrapping_sub, andwrapping_mul. - A conversion to an integer type that does not fit traps.
- Bit operators
&,|,^,~,<<, and>>work on every integer type. - A number literal takes a sized type from its context, as in
let b: u8 = 200.
let a: u8 = 250let b = a + 5 # u8, since a isprint bprint 17 // 5 # 3print -7 % 3 # 2let name = "Ada"let age = 36print "Hi {name}, next year you are {age + 1}"print "use {{ for a literal brace"
let block = """ Multi-line text. Common indentation is removed. """print blockInterpolation is the only way to join text, so + does not work on text. Any value can be interpolated, and reads as print shows it. Escapes are \n, \t, \r, \\, and \".
Length, slicing, and iteration count Unicode characters:
let word = "héllo"print word.len() # 5print word[1..3] # élfor letter in word print letterA single index such as word[0] is a compile error: write word[0..1]. There is no separate character type.
Text also has these tasks:
print "a,b,,c".split(",") # ["a", "b", "", "c"]print "Straße".upper() # STRASSEprint "a.lcd".ends_with(".lcd") # true, and starts_with tooprint "héllo".contains("ll") # trueprint "héllo".find("l") # 2, an int? that is none if absentprint " hi ".trim() # hiprint "-42".to_int() # -42, an int? that is none if invalidprint "abc".bytes() # the UTF-8 bytes, a list[byte]==, <, and the other comparisons work on text, in Unicode code point order.