Types
Nylon's built-in types are the signed and unsigned integers, the floats, boolean, utf (a single Unicode code
point) and string:
i8 - A signed byte value
i16 - A signed short value
i32 - A signed integer value
i64 - A signed long value
u8 - An unsigned byte value
u16 - An unsigned short value
u32 - An unsigned integer value
u64 - An unsigned long value
f32 - A 32-bit float
f64 - A 64-bit float
boolean - true or false.
utf - A utf-32 code point.
string - Text.
bytes - A byte array (the same as u8[]).
Literals
A whole number can be written in decimal, hex, binary or octal, with _ to group digits. A bare whole number is an
i32 and a bare decimal an f64, unless the place it goes gives it a type:
let decimal = 42
let hex = 0x2A
let binary = 0b101010
let octal = 0o52
let million = 1_000_000
let half = 0.5
let precise : f32 = 1.5
Converting
Values are converted between these types explicitly, by calling the type's name:
let integer = i32(1)
let long = i64(integer)
let real = f64(integer)
let text = string(integer)
A smaller number widens to a bigger type of its kind by itself, so an i32 plus an i64 is an i64, and an f32
can go where an f64 is wanted. Anything else is written out: going from i64 to i32, or from a whole number to a
float. A whole number that overflows its type wraps around.
Strings
string is a trait rather than one fixed implementation, which gives the compiler freedom to choose the most efficient
representation. ${...} puts a value into a string:
let name = "Ada"
let count = 3
let text : string = "${name} has ${count} messages"
none, T?, and unit
A type that may have no value is written with a ?. The absence of a value is none, similar to null in other
languages:
let name : string? = "Ada"
let nothing : string? = none
let name : string? = "Ada"
# Both of these checks are equivalent:
if (name?) { println("there is a name") }
if (name != none) { println("there is a name") }
T? works for any type, numbers included:
let reading : i32? = none
let fallback = reading ?: 0
unit represents "no meaningful information", which is distinct from the absence of a value:
# unit is a singleton value that exists but carries no information.
let instance = unit