Values are objects, like for example booleans, integers, or arrays. Values are typed.
Booleans
The two boolean values true and false have the type Bool.
Numeric Literals
Numbers can be written in various bases. Numbers are assumed to be decimal by default. Non-decimal literals have a specific prefix.
| Numeral system | Prefix | Characters |
|---|---|---|
| Decimal | None | one or more numbers (0 to 9) |
| Binary | 0b | one or more zeros or ones (0 or 1) |
| Octal | 0o | one or more numbers in the range 0 to 7 |
| Hexadecimal | 0x | one or more numbers, or characters a to f, lowercase or uppercase |
// A decimal number
//
1234567890 // is `1234567890`
// A binary number
//
0b101010 // is `42`
// An octal number
//
0o12345670 // is `2739128`
// A hexadecimal number
//
0x1234567890ABCabc // is `1311768467294898876`
// Invalid: unsupported prefix 0z
//
0z0
// A decimal number with leading zeros. Not an octal number!
00123 // is `123`
// A binary number with several trailing zeros.
0b001000 // is `8`Decimal numbers may contain underscores (_) to logically separate components.
let largeNumber = 1_000_000
// Invalid: Value is not a number literal, but a variable.
let notNumber = _123Underscores are allowed for all numeral systems.
let binaryNumber = 0b10_11_01Integers
Integers are numbers without a fractional part. They are either signed (positive, zero, or negative) or unsigned (positive or zero).
Signed integer types which check for overflow and underflow have an Int prefix
and can represent values in the following ranges:
Int8: -2^7 through 2^7 − 1 (-128 through 127)Int16: -2^15 through 2^15 − 1 (-32768 through 32767)Int32: -2^31 through 2^31 − 1 (-2147483648 through 2147483647)Int64: -2^63 through 2^63 − 1 (-9223372036854775808 through 9223372036854775807)Int128: -2^127 through 2^127 − 1Int256: -2^255 through 2^255 − 1
Unsigned integer types which check for overflow and underflow have a UInt prefix
and can represent values in the following ranges:
UInt8: 0 through 2^8 − 1 (255)UInt16: 0 through 2^16 − 1 (65535)UInt32: 0 through 2^32 − 1 (4294967295)UInt64: 0 through 2^64 − 1 (18446744073709551615)UInt128: 0 through 2^128 − 1UInt256: 0 through 2^256 − 1
Unsigned integer types which do not check for overflow and underflow,
i.e. wrap around, have the Word prefix
and can represent values in the following ranges:
Word8: 0 through 2^8 − 1 (255)Word16: 0 through 2^16 − 1 (65535)Word32: 0 through 2^32 − 1 (4294967295)Word64: 0 through 2^64 − 1 (18446744073709551615)Word128: 0 through 2^128 − 1 (340282366920938463463374607431768211455)Word256: 0 through 2^256 − 1 (115792089237316195423570985008687907853269984665640564039457584007913129639935)
The types are independent types, i.e. not subtypes of each other.
See the section about arithmetic operators for further information about the behavior of the different integer types.
// Declare a constant that has type `UInt8` and the value 10.
let smallNumber: UInt8 = 10// Invalid: negative literal cannot be used as an unsigned integer
//
let invalidNumber: UInt8 = -10In addition, the arbitrary precision integer type Int is provided.
let veryLargeNumber: Int = 10000000000000000000000000000000Integer literals are inferred to have type Int,
or if the literal occurs in a position that expects an explicit type,
e.g. in a variable declaration with an explicit type annotation.
let someNumber = 123
// `someNumber` has type `Int`Negative integers are encoded in two’s complement representation.
Integer types are not converted automatically. Types must be explicitly converted, which can be done by calling the constructor of the type with the integer type.
let x: Int8 = 1
let y: Int16 = 2
// Invalid: the types of the operands, `Int8` and `Int16` are incompatible.
let z = x + y
// Explicitly convert `x` from `Int8` to `Int16`.
let a = Int16(x) + y
// `a` has type `Int16`
// Invalid: The integer literal is expected to be of type `Int8`,
// but the large integer literal does not fit in the range of `Int8`.
//
let b = x + 1000000000000000000000000Integer Functions
Integers have multiple built-in functions you can use.
-
view fun toString(): StringReturns the string representation of the integer.
let answer = 42 answer.toString() // is "42" -
view fun toBigEndianBytes(): [UInt8]Returns the byte array representation (
[UInt8]) in big-endian order of the integer.let largeNumber = 1234567890 largeNumber.toBigEndianBytes() // is `[73, 150, 2, 210]`
All integer types support the following functions:
-
view fun T.fromString(_ input: String): T?Attempts to parse an integer value from a base-10 encoded string, returning
nilif the string is invalid.For a given integer
nof typeT,T.fromString(n.toString())is equivalent to wrappingnup in an optional.Strings are invalid if:
- they contain non-digit characters
- they don’t fit in the target type
For signed integer types like
Int64, andInt, the string may optionally begin with+or-sign prefix.For unsigned integer types like
Word64,UInt64, andUInt, sign prefices are not allowed.Examples:
let fortyTwo: Int64? = Int64.fromString("42") // ok let twenty: UInt? = UInt.fromString("20") // ok let nilWord: Word8? = Word8.fromString("1024") // nil, out of bounds let negTwenty: Int? = Int.fromString("-20") // ok -
view fun T.fromBigEndianBytes(_ bytes: [UInt8]): T?Attempts to parse an integer value from a byte array representation (
[UInt8]) in big-endian order, returningnilif the input bytes are invalid.For a given integer
nof typeT,T.fromBigEndianBytes(n.toBigEndianBytes())is equivalent to wrappingnup in an optional.The bytes are invalid if:
- length of the bytes array exceeds the number of bytes needed for the target type
- they don’t fit in the target type
Examples:
let fortyTwo: UInt32? = UInt32.fromBigEndianBytes([42]) // ok let twenty: UInt? = UInt.fromBigEndianBytes([0, 0, 20]) // ok let nilWord: Word8? = Word8.fromBigEndianBytes("[0, 22, 0, 0, 0, 0, 0, 0, 0]") // nil, out of bounds let nilWord2: Word8? = Word8.fromBigEndianBytes("[0, 0]") // nil, size (2) exceeds number of bytes needed for Word8 (1) let negativeNumber: Int64? = Int64.fromBigEndianBytes([128, 0, 0, 0, 0, 0, 0, 1]) // ok -9223372036854775807
Fixed-Point Numbers
:::warning[🚧 Status]
Currently only the 64-bit wide Fix64 and UFix64 types are available.
More fixed-point number types will be added in a future release.
:::
Fixed-point numbers are useful for representing fractional values. They have a fixed number of digits after decimal point.
They are essentially integers which are scaled by a factor. For example, the value 1.23 can be represented as 1230 with a scaling factor of 1/1000. The scaling factor is the same for all values of the same type and stays the same during calculations.
Fixed-point numbers in Cadence have a scaling factor with a power of 10, instead of a power of 2, i.e. they are decimal, not binary.
Signed fixed-point number types have the prefix Fix,
have the following factors, and can represent values in the following ranges:
Fix64: Factor 1/100,000,000; -92233720368.54775808 through 92233720368.54775807
Unsigned fixed-point number types have the prefix UFix,
have the following factors, and can represent values in the following ranges:
UFix64: Factor 1/100,000,000; 0.0 through 184467440737.09551615
Fixed-Point Number Functions
Fixed-Point numbers have multiple built-in functions you can use.
-
view fun toString(): StringReturns the string representation of the fixed-point number.
let fix = 1.23 fix.toString() // is "1.23000000" -
view fun toBigEndianBytes(): [UInt8]Returns the byte array representation (
[UInt8]) in big-endian order of the fixed-point number.let fix = 1.23 fix.toBigEndianBytes() // is `[0, 0, 0, 0, 7, 84, 212, 192]`
All fixed-point types support the following functions:
-
view fun T.fromString(_ input: String): T?Attempts to parse a fixed-point value from a base-10 encoded string, returning
nilif the string is invalid.For a given fixed-point numeral
nof typeT,T.fromString(n.toString())is equivalent to wrappingnup in anoptional.Strings are invalid if:
- they contain non-digit characters.
- they don’t fit in the target type.
- they’re missing a decimal or fractional component. For example, both “0.” and “.1” are invalid strings, but “0.1” is accepted.
For signed types like
Fix64, the string may optionally begin with+or-sign prefix.For unsigned types like
UFix64, sign prefices are not allowed.Examples:
let nil1: UFix64? = UFix64.fromString("0.") // nil, fractional part is required let nil2: UFix64? = UFix64.fromString(".1") // nil, decimal part is required let smol: UFix64? = UFix64.fromString("0.1") // ok let smolString: String = "-0.1" let nil3: UFix64? = UFix64.fromString(smolString) // nil, unsigned types don't allow a sign prefix let smolFix64: Fix64? = Fix64.fromString(smolString) // ok -
view fun T.fromBigEndianBytes(_ bytes: [UInt8]): T?Attempts to parse an integer value from a byte array representation (
[UInt8]) in big-endian order, returningnilif the input bytes are invalid.For a given integer
nof typeT,T.fromBigEndianBytes(n.toBigEndianBytes())is equivalent to wrappingnup in an optional.The bytes are invalid if:
- length of the bytes array exceeds the number of bytes needed for the target type
- they don’t fit in the target type
Examples:
let fortyTwo: UFix64? = UFix64.fromBigEndianBytes([0, 0, 0, 0, 250, 86, 234, 0]) // ok, 42.0 let nilWord: UFix64? = UFix64.fromBigEndianBytes("[100, 22, 0, 0, 0, 0, 0, 0, 0]") // nil, out of bounds let nilWord2: Fix64? = Fix64.fromBigEndianBytes("[0, 22, 0, 0, 0, 0, 0, 0, 0]") // // nil, size (9) exceeds number of bytes needed for Fix64 (8) let negativeNumber: Fix64? = Fix64.fromBigEndianBytes([255, 255, 255, 255, 250, 10, 31, 0]) // ok, -1
Minimum and maximum values
The minimum and maximum values for all integer and fixed-point number types are available through the fields min and max.
For example:
let max = UInt8.max
// `max` is 255, the maximum value of the type `UInt8`let max = UFix64.max
// `max` is 184467440737.09551615, the maximum value of the type `UFix64`Saturation Arithmetic
Integers and fixed-point numbers support saturation arithmetic: Arithmetic operations, such as addition or multiplications, are saturating at the numeric bounds instead of overflowing.
If the result of an operation is greater than the maximum value of the operands’ type, the maximum is returned. If the result is lower than the minimum of the operands’ type, the minimum is returned.
Saturating addition, subtraction, multiplication, and division are provided as functions with the prefix saturating:
-
Int8,Int16,Int32,Int64,Int128,Int256,Fix64:saturatingAddsaturatingSubtractsaturatingMultiplysaturatingDivide
-
Int:- none
-
UInt8,UInt16,UInt32,UInt64,UInt128,UInt256,UFix64:saturatingAddsaturatingSubtractsaturatingMultiply
-
UInt:saturatingSubtract
let a: UInt8 = 200
let b: UInt8 = 100
let result = a.saturatingAdd(b)
// `result` is 255, the maximum value of the type `UInt8`Floating-Point Numbers
There is no support for floating point numbers.
Smart Contracts are not intended to work with values with error margins and therefore floating point arithmetic is not appropriate here.
Instead, consider using fixed point numbers.
Addresses
The type Address represents an address.
Addresses are unsigned integers with a size of 64 bits (8 bytes).
Hexadecimal integer literals can be used to create address values.
// Declare a constant that has type `Address`.
//
let someAddress: Address = 0x436164656E636521
// Invalid: Initial value is not compatible with type `Address`,
// it is not a number.
//
let notAnAddress: Address = ""
// Invalid: Initial value is not compatible with type `Address`.
// The integer literal is valid, however, it is larger than 64 bits.
//
let alsoNotAnAddress: Address = 0x436164656E63652146757265766572Integer literals are not inferred to be an address.
// Declare a number. Even though it happens to be a valid address,
// it is not inferred as it.
//
let aNumber = 0x436164656E636521
// `aNumber` has type `Int`Address can also be created using a byte array or string.
// Declare an address with hex representation as 0x436164656E636521.
let someAddress: Address = Address.fromBytes([67, 97, 100, 101, 110, 99, 101, 33])
// Invalid: Provided value is not compatible with type `Address`. The function panics.
let invalidAddress: Address = Address.fromBytes([12, 34, 56, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111])
// Declare an address with the string representation as "0x436164656E636521".
let addressFromString: Address? = Address.fromString("0x436164656E636521")
// Invalid: Provided value does not have the "0x" prefix. Returns Nil
let addressFromStringWithoutPrefix: Address? = Address.fromString("436164656E636521")
// Invalid: Provided value is an invalid hex string. Return Nil.
let invalidAddressForInvalidHex: Address? = Address.fromString("0xZZZ")
// Invalid: Provided value is larger than 64 bits. Return Nil.
let invalidAddressForOverflow: Address? = Address.fromString("0x436164656E63652146757265766572")Address Functions
Addresses have multiple built-in functions you can use.
-
view fun toString(): StringReturns the string representation of the address. The result has a
0xprefix and is zero-padded.let someAddress: Address = 0x436164656E636521 someAddress.toString() // is "0x436164656E636521" let shortAddress: Address = 0x1 shortAddress.toString() // is "0x0000000000000001" -
view fun toBytes(): [UInt8]Returns the byte array representation (
[UInt8]) of the address.let someAddress: Address = 0x436164656E636521 someAddress.toBytes() // is `[67, 97, 100, 101, 110, 99, 101, 33]`
AnyStruct and AnyResource
AnyStruct is the top type of all non-resource types,
i.e., all non-resource types are a subtype of it.
AnyResource is the top type of all resource types.
// Declare a variable that has the type `AnyStruct`.
// Any non-resource typed value can be assigned to it, for example an integer,
// but not resource-typed values.
//
var someStruct: AnyStruct = 1
// Assign a value with a different non-resource type, `Bool`.
someStruct = true
// Declare a structure named `TestStruct`, create an instance of it,
// and assign it to the `AnyStruct`-typed variable
//
struct TestStruct {}
let testStruct = TestStruct()
someStruct = testStruct
// Declare a resource named `TestResource`
resource TestResource {}
// Declare a variable that has the type `AnyResource`.
// Any resource-typed value can be assigned to it,
// but not non-resource typed values.
//
var someResource: @AnyResource <- create TestResource()
// Invalid: Resource-typed values can not be assigned
// to `AnyStruct`-typed variables
//
someStruct <- create TestResource()
// Invalid: Non-resource typed values can not be assigned
// to `AnyResource`-typed variables
//
someResource = 1However, using AnyStruct and AnyResource does not opt-out of type checking.
It is invalid to access fields and call functions on these types,
as they have no fields and functions.
// Declare a variable that has the type `AnyStruct`.
// The initial value is an integer,
// but the variable still has the explicit type `AnyStruct`.
//
let a: AnyStruct = 1
// Invalid: Operator cannot be used for an `AnyStruct` value (`a`, left-hand side)
// and an `Int` value (`2`, right-hand side).
//
a + 2AnyStruct and AnyResource may be used like other types,
for example, they may be the element type of arrays
or be the element type of an optional type.
// Declare a variable that has the type `[AnyStruct]`,
// i.e. an array of elements of any non-resource type.
//
let anyValues: [AnyStruct] = [1, "2", true]
// Declare a variable that has the type `AnyStruct?`,
// i.e. an optional type of any non-resource type.
//
var maybeSomething: AnyStruct? = 42
maybeSomething = "twenty-four"
maybeSomething = nilAnyStruct is also the super-type of all non-resource optional types,
and AnyResource is the super-type of all resource optional types.
let maybeInt: Int? = 1
let anything: AnyStruct = maybeIntConditional downcasting allows coercing
a value which has the type AnyStruct or AnyResource back to its original type.
Optionals
Optionals are values which can represent the absence of a value. Optionals have two cases: either there is a value, or there is nothing.
An optional type is declared using the ? suffix for another type.
For example, Int is a non-optional integer, and Int? is an optional integer,
i.e. either nothing, or an integer.
The value representing nothing is nil.
// Declare a constant which has an optional integer type,
// with nil as its initial value.
//
let a: Int? = nil
// Declare a constant which has an optional integer type,
// with 42 as its initial value.
//
let b: Int? = 42
// Invalid: `b` has type `Int?`, which does not support arithmetic.
b + 23
// Invalid: Declare a constant with a non-optional integer type `Int`,
// but the initial value is `nil`, which in this context has type `Int?`.
//
let x: Int = nilOptionals can be created for any value, not just for literals.
// Declare a constant which has a non-optional integer type,
// with 1 as its initial value.
//
let x = 1
// Declare a constant which has an optional integer type.
// An optional with the value of `x` is created.
//
let y: Int? = x
// Declare a variable which has an optional any type, i.e. the variable
// may be `nil`, or any other value.
// An optional with the value of `x` is created.
//
var z: AnyStruct? = xA non-optional type is a subtype of its optional type.
var a: Int? = nil
let b = 2
a = b
// `a` is `2`Optional types may be contained in other types, for example arrays or even optionals.
// Declare a constant which has an array type of optional integers.
let xs: [Int?] = [1, nil, 2, nil]
// Declare a constant which has a double optional type.
//
let doubleOptional: Int?? = nilSee the optional operators section for information on how to work with optionals.
Never
Never is the bottom type, i.e., it is a subtype of all types.
There is no value that has type Never.
Never can be used as the return type for functions that never return normally.
For example, it is the return type of the function panic.
// Declare a function named `crashAndBurn` which will never return,
// because it calls the function named `panic`, which never returns.
//
fun crashAndBurn(): Never {
panic("An unrecoverable error occurred")
}
// Invalid: Declare a constant with a `Never` type, but the initial value is an integer.
//
let x: Never = 1
// Invalid: Declare a function which returns an invalid return value `nil`,
// which is not a value of type `Never`.
//
fun returnNever(): Never {
return nil
}Strings and Characters
Strings are collections of characters.
Strings have the type String, and characters have the type Character.
Strings can be used to work with text in a Unicode-compliant way.
Strings are immutable.
String and character literals are enclosed in double quotation marks (").
let someString = "Hello, world!"String literals may contain escape sequences. An escape sequence starts with a backslash (\):
\0: Null character\\: Backslash\t: Horizontal tab\n: Line feed\r: Carriage return\": Double quotation mark\': Single quotation mark\u: A Unicode scalar value, written as\u{x}, wherexis a 1–8 digit hexadecimal number which needs to be a valid Unicode scalar value, i.e., in the range 0 to 0xD7FF and 0xE000 to 0x10FFFF inclusive
// Declare a constant which contains two lines of text
// (separated by the line feed character `\n`), and ends
// with a thumbs up emoji, which has code point U+1F44D (0x1F44D).
//
let thumbsUpText =
"This is the first line.\nThis is the second line with an emoji: \u{1F44D}"The type Character represents a single, human-readable character.
Characters are extended grapheme clusters,
which consist of one or more Unicode scalars.
For example, the single character ü can be represented
in several ways in Unicode.
First, it can be represented by a single Unicode scalar value ü
(“LATIN SMALL LETTER U WITH DIAERESIS”, code point U+00FC).
Second, the same single character can be represented
by two Unicode scalar values:
u (“LATIN SMALL LETTER U”, code point U+0075),
and “COMBINING DIAERESIS” (code point U+0308).
The combining Unicode scalar value is applied to the scalar before it,
which turns a u into a ü.
Still, both variants represent the same human-readable character ü.
let singleScalar: Character = "\u{FC}"
// `singleScalar` is `ü`
let twoScalars: Character = "\u{75}\u{308}"
// `twoScalars` is `ü`Another example where multiple Unicode scalar values are rendered as a single, human-readable character is a flag emoji. These emojis consist of two “REGIONAL INDICATOR SYMBOL LETTER” Unicode scalar values.
// Declare a constant for a string with a single character, the emoji
// for the Canadian flag, which consists of two Unicode scalar values:
// - REGIONAL INDICATOR SYMBOL LETTER C (U+1F1E8)
// - REGIONAL INDICATOR SYMBOL LETTER A (U+1F1E6)
//
let canadianFlag: Character = "\u{1F1E8}\u{1F1E6}"
// `canadianFlag` is `🇨🇦`String Fields and Functions
Strings have multiple built-in functions you can use:
-
let length: IntReturns the number of characters in the string as an integer.
let example = "hello" // Find the number of elements of the string. let length = example.length // `length` is `5` -
let utf8: [UInt8]The byte array of the UTF-8 encoding
let flowers = "Flowers \u{1F490}" let bytes = flowers.utf8 // `bytes` is `[70, 108, 111, 119, 101, 114, 115, 32, 240, 159, 146, 144]` -
view fun concat(_ other: String): StringConcatenates the string
otherto the end of the original string, but does not modify the original string. This function creates a new string whose length is the sum of the lengths of the string the function is called on and the string given as a parameter.let example = "hello" let new = "world" // Concatenate the new string onto the example string and return the new string. let helloWorld = example.concat(new) // `helloWorld` is now `"helloworld"` -
view fun slice(from: Int, upTo: Int): StringReturns a string slice of the characters in the given string from start index
fromup to, but not including, the end indexupTo. This function creates a new string whose length isupTo - from. It does not modify the original string. If either of the parameters are out of the bounds of the string, or the indices are invalid (from > upTo), then the function will fail.let example = "helloworld" // Create a new slice of part of the original string. let slice = example.slice(from: 3, upTo: 6) // `slice` is now `"low"` // Run-time error: Out of bounds index, the program aborts. let outOfBounds = example.slice(from: 2, upTo: 10) // Run-time error: Invalid indices, the program aborts. let invalidIndices = example.slice(from: 2, upTo: 1) -
view fun decodeHex(): [UInt8]Returns an array containing the bytes represented by the given hexadecimal string.
The given string must only contain hexadecimal characters and must have an even length. If the string is malformed, the program aborts
let example = "436164656e636521" example.decodeHex() // is `[67, 97, 100, 101, 110, 99, 101, 33]` -
view fun toLower(): StringReturns a string where all upper case letters are replaced with lowercase characters
let example = "Flowers" example.toLower() // is `flowers` -
view fun replaceAll(of: String, with: String): StringReturns a string where all occurences of
ofare replaced withwith. Ifofis empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding k+1 replacements for a string of length k.let example = "abababa" example.replaceAll(of: "a", with: "o") // is `obobobo` -
view fun split(separator: String): [String]Returns the variable-sized array of strings created splitting the receiver string on the
separator.let example = "hello world" example.split(separator: " ") // is `["hello", "world"]`
The String type also provides the following functions:
-
view fun String.encodeHex(_ data: [UInt8]): StringReturns a hexadecimal string for the given byte array
let data = [1 as UInt8, 2, 3, 0xCA, 0xDE] String.encodeHex(data) // is `"010203cade"` -
view fun String.join(_ strings: [String], separator: String): StringReturns the string created by joining the array of
stringswith the providedseparator.let strings = ["hello", "world"] String.join(strings, " ") // is "hello world"
Strings are also indexable, returning a Character value.
let str = "abc"
let c = str[0] // is the Character "a"-
view fun String.fromUTF8(_ input: [UInt8]): String?Attempts to convert a UTF-8 encoded byte array into a
String. This function returnsnilif the byte array contains invalid UTF-8, such as incomplete codepoint sequences or undefined graphemes.For a given string
s,String.fromUTF8(s.utf8)is equivalent to wrappingsup in an optional.
Character Fields and Functions
Character values can be converted into String values using the toString function:
-
view fun toString(): String`Returns the string representation of the character.
let c: Character = "x" c.toString() // is "x" -
view fun String.fromCharacters(_ characters: [Character]): StringBuilds a new
Stringvalue from an array ofCharacters. BecauseStrings are immutable, this operation makes a copy of the input array.let rawUwU: [Character] = ["U", "w", "U"] let uwu: String = String.fromCharacters(rawUwU) // "UwU" -
let utf8: [UInt8]The byte array of the UTF-8 encoding
let a: Character = "a" let a_bytes = a.utf8 // `a_bytes` is `[97]` let bouquet: Character = "\u{1F490}" let bouquet_bytes = bouquet.utf8 // `bouquet_bytes` is `[240, 159, 146, 144]`
Arrays
Arrays are mutable, ordered collections of values.
Arrays may contain a value multiple times.
Array literals start with an opening square bracket [ and end with a closing square bracket ].
// An empty array
//
[]
// An array with integers
//
[1, 2, 3]Array Types
Arrays either have a fixed size or are variably sized, i.e., elements can be added and removed.
Fixed-size array types have the form [T; N], where T is the element type,
and N is the size of the array. N has to be statically known, meaning
that it needs to be an integer literal.
For example, a fixed-size array of 3 Int8 elements has the type [Int8; 3].
Variable-size array types have the form [T], where T is the element type.
For example, the type [Int16] specifies a variable-size array of elements that have type Int16.
All values in an array must have a type which is a subtype of the array’s element type (T).
It is important to understand that arrays are value types and are only ever copied when used as an initial value for a constant or variable, when assigning to a variable, when used as function argument, or when returned from a function call.
let size = 2
// Invalid: Array-size must be an integer literal
let numbers: [Int; size] = []
// Declare a fixed-sized array of integers
// which always contains exactly two elements.
//
let array: [Int8; 2] = [1, 2]
// Declare a fixed-sized array of fixed-sized arrays of integers.
// The inner arrays always contain exactly three elements,
// the outer array always contains two elements.
//
let arrays: [[Int16; 3]; 2] = [
[1, 2, 3],
[4, 5, 6]
]
// Declare a variable length array of integers
var variableLengthArray: [Int] = []
// Mixing values with different types is possible
// by declaring the expected array type
// with the common supertype of all values.
//
let mixedValues: [AnyStruct] = ["some string", 42]Array types are covariant in their element types.
For example, [Int] is a subtype of [AnyStruct].
This is safe because arrays are value types and not reference types.
Array Indexing
To get the element of an array at a specific index, the indexing syntax can be used:
The array is followed by an opening square bracket [, the indexing value,
and ends with a closing square bracket ].
Indexes start at 0 for the first element in the array.
Accessing an element which is out of bounds results in a fatal error at run-time and aborts the program.
// Declare an array of integers.
let numbers = [42, 23]
// Get the first number of the array.
//
numbers[0] // is `42`
// Get the second number of the array.
//
numbers[1] // is `23`
// Run-time error: Index 2 is out of bounds, the program aborts.
//
numbers[2]// Declare an array of arrays of integers, i.e. the type is `[[Int]]`.
let arrays = [[1, 2], [3, 4]]
// Get the first number of the second array.
//
arrays[1][0] // is `3`To set an element of an array at a specific index, the indexing syntax can be used as well.
// Declare an array of integers.
let numbers = [42, 23]
// Change the second number in the array.
//
// NOTE: The declaration `numbers` is constant, which means that
// the *name* is constant, not the *value* – the value, i.e. the array,
// is mutable and can be changed.
//
numbers[1] = 2
// `numbers` is `[42, 2]`Array Fields and Functions
Arrays have multiple built-in fields and functions that can be used to get information about and manipulate the contents of the array.
The field length, and the functions concat, and contains
are available for both variable-sized and fixed-sized or variable-sized arrays.
-
let length: IntThe number of elements in the array.
// Declare an array of integers. let numbers = [42, 23, 31, 12] // Find the number of elements of the array. let length = numbers.length // `length` is `4` -
access(all) view fun concat(_ array: T): TConcatenates the parameter
arrayto the end of the array the function is called on, but does not modify that array.Both arrays must be the same type
T.This function creates a new array whose length is the sum of the length of the array the function is called on and the length of the array given as the parameter.
// Declare two arrays of integers. let numbers = [42, 23, 31, 12] let moreNumbers = [11, 27] // Concatenate the array `moreNumbers` to the array `numbers` // and declare a new variable for the result. // let allNumbers = numbers.concat(moreNumbers) // `allNumbers` is `[42, 23, 31, 12, 11, 27]` // `numbers` is still `[42, 23, 31, 12]` // `moreNumbers` is still `[11, 27]` -
access(all) view fun contains(_ element: T): BoolReturns true if the given element of type
Tis in the array.// Declare an array of integers. let numbers = [42, 23, 31, 12] // Check if the array contains 11. let containsEleven = numbers.contains(11) // `containsEleven` is `false` // Check if the array contains 12. let containsTwelve = numbers.contains(12) // `containsTwelve` is `true` // Invalid: Check if the array contains the string "Kitty". // This results in a type error, as the array only contains integers. // let containsKitty = numbers.contains("Kitty") -
access(all) view fun firstIndex(of: T): Int?Returns the index of the first element matching the given object in the array, nil if no match. Available if
Tis not resource-kinded and equatable.// Declare an array of integers. let numbers = [42, 23, 31, 12] // Check if the array contains 31 let index = numbers.firstIndex(of: 31) // `index` is 2 // Check if the array contains 22 let index = numbers.firstIndex(of: 22) // `index` is nil -
access(all) view fun slice(from: Int, upTo: Int): [T]Returns an array slice of the elements in the given array from start index
fromup to, but not including, the end indexupTo. This function creates a new array whose length isupTo - from. It does not modify the original array. If either of the parameters are out of the bounds of the array, or the indices are invalid (from > upTo), then the function will fail.let example = [1, 2, 3, 4] // Create a new slice of part of the original array. let slice = example.slice(from: 1, upTo: 3) // `slice` is now `[2, 3]` // Run-time error: Out of bounds index, the program aborts. let outOfBounds = example.slice(from: 2, upTo: 10) // Run-time error: Invalid indices, the program aborts. let invalidIndices = example.slice(from: 2, upTo: 1) -
access(all) view fun reverse(): [T]Returns a new array with contents in the reversed order. Available if
Tis not resource-kinded.let example = [1, 2, 3, 4] // Create a new array which is the reverse of the original array. let reversedExample = example.reverse() // `reversedExample` is now `[4, 3, 2, 1]`access(all) view fun reverse(): [T; N]Returns a new fixed-sized array of same size with contents in the reversed order.
let fixedSizedExample: [String; 3] = ["ABC", "XYZ", "PQR"] // Create a new array which is the reverse of the original array. let fixedArrayReversedExample = fixedSizedExample.reverse() // `fixedArrayReversedExample` is now `["PQR", "XYZ", "ABC"]` -
access(all) fun map(_ f: fun(T): U): [U]Returns a new array whose elements are produced by applying the mapper function on each element of the original array. Available if
Tis not resource-kinded.let example = [1, 2, 3] let trueForEven = fun (_ x: Int): Bool { return x % 2 == 0 } let mappedExample: [Bool] = example.map(trueForEven) // `mappedExample` is `[False, True, False]` // `example` still remains as `[1, 2, 3]` // Invalid: Map using a function which accepts a different type. // This results in a type error, as the array contains `Int` values while function accepts // `Int64`. let functionAcceptingInt64 = fun (_ x: Int64): Bool { return x % 2 == 0 } let invalidMapFunctionExample = example.map(functionAcceptingInt64)mapfunction is also available for fixed-sized arrays:access(all) fun map(_ f: fun(T): U): [U; N]Returns a new fixed-sized array whose elements are produced by applying the mapper function on each element of the original array. Available if
Tis not resource-kinded.let fixedSizedExample: [String; 3] = ["ABC", "XYZYX", "PQR"] let lengthOfString = fun (_ x: String): Int { return x.length } let fixedArrayMappedExample = fixedSizedExample.map(lengthOfString) // `fixedArrayMappedExample` is now `[3, 5, 3]` // `fixedSizedExample` still remains as ["ABC", "XYZYX", "PQR"] // Invalid: Map using a function which accepts a different type. // This results in a type error, as the array contains `String` values while function accepts // `Bool`. let functionAcceptingBool = fun (_ x: Bool): Int { return 0 } let invalidMapFunctionExample = fixedSizedExample.map(functionAcceptingBool) -
access(all) view fun filter(_ f: view fun(T): Bool): [T]Returns a new array whose elements are filtered by applying the filter function on each element of the original array. Available if
Tis not resource-kinded.let example = [1, 2, 3] let trueForEven = fun (_ x: Int): Bool { return x % 2 == 0 } let filteredExample: [Int] = example.filter(trueForEven) // `filteredExample` is `[2]` // `example` still remains as `[1, 2, 3]` // Invalid: Filter using a function which accepts a different type. // This results in a type error, as the array contains `Int` values while function accepts // `Int64`. let functionAcceptingInt64 = fun (_ x: Int64): Bool { return x % 2 == 0 } let invalidFilterFunctionExample = example.filter(functionAcceptingInt64)filterfunction is also available for fixed-sized arrays:access(all) view fun filter(_ f: view fun(T): Bool): [T]Returns a new variable-sized array whose elements are filtered by applying the filter function on each element of the original array. Available if
Tis not resource-kinded.let fixedSizedExample: [String; 3] = ["AB", "XYZYX", "PQR"] let lengthOfStringGreaterThanTwo = fun (_ x: String): Bool { return x.length > 2 } let fixedArrayFilteredExample = fixedSizedExample.filter(lengthOfStringGreaterThanTwo) // `fixedArrayFilteredExample` is `["XYZYX", "PQR"]` // `fixedSizedExample` still remains as ["AB", "XYZYX", "PQR"] // Invalid: Filter using a function which accepts a different type. // This results in a type error, as the array contains `String` values while function accepts // `Bool`. let functionAcceptingBool = fun (_ x: Bool): Bool { return True } let invalidFilterFunctionExample = fixedSizedExample.filter(functionAcceptingBool)
Variable-size Array Functions
The following functions can only be used on variable-sized arrays. It is invalid to use one of these functions on a fixed-sized array.
-
access(Mutate | Insert) fun append(_ element: T): VoidAdds the new element
elementof typeTto the end of the array.The new element must be the same type as all the other elements in the array.
This function mutates the array.
// Declare an array of integers. let numbers = [42, 23, 31, 12] // Add a new element to the array. numbers.append(20) // `numbers` is now `[42, 23, 31, 12, 20]` // Invalid: The parameter has the wrong type `String`. numbers.append("SneakyString") -
access(Mutate | Insert) fun appendAll(_ array: T): VoidAdds all the elements from
arrayto the end of the array the function is called on.Both arrays must be the same type
T.This function mutates the array.
// Declare an array of integers. let numbers = [42, 23] // Add new elements to the array. numbers.appendAll([31, 12, 20]) // `numbers` is now `[42, 23, 31, 12, 20]` // Invalid: The parameter has the wrong type `[String]`. numbers.appendAll(["Sneaky", "String"]) -
access(Mutate | Insert) fun insert(at: Int, _ element: T): VoidInserts the new element
elementof typeTat the givenindexof the array.The new element must be of the same type as the other elements in the array.
The
indexmust be within the bounds of the array. If the index is outside the bounds, the program aborts.The existing element at the supplied index is not overwritten.
All the elements after the new inserted element are shifted to the right by one.
This function mutates the array.
// Declare an array of integers. let numbers = [42, 23, 31, 12] // Insert a new element at position 1 of the array. numbers.insert(at: 1, 20) // `numbers` is now `[42, 20, 23, 31, 12]` // Run-time error: Out of bounds index, the program aborts. numbers.insert(at: 12, 39) -
access(Mutate | Remove) fun remove(at: Int): TRemoves the element at the given
indexfrom the array and returns it.The
indexmust be within the bounds of the array. If the index is outside the bounds, the program aborts.This function mutates the array.
// Declare an array of integers. let numbers = [42, 23, 31] // Remove element at position 1 of the array. let twentyThree = numbers.remove(at: 1) // `numbers` is now `[42, 31]` // `twentyThree` is `23` // Run-time error: Out of bounds index, the program aborts. numbers.remove(at: 19) -
access(Mutate | Remove) fun removeFirst(): TRemoves the first element from the array and returns it.
The array must not be empty. If the array is empty, the program aborts.
This function mutates the array.
// Declare an array of integers. let numbers = [42, 23] // Remove the first element of the array. let fortytwo = numbers.removeFirst() // `numbers` is now `[23]` // `fortywo` is `42` // Remove the first element of the array. let twentyThree = numbers.removeFirst() // `numbers` is now `[]` // `twentyThree` is `23` // Run-time error: The array is empty, the program aborts. numbers.removeFirst() -
access(Mutate | Remove) fun removeLast(): TRemoves the last element from the array and returns it.
The array must not be empty. If the array is empty, the program aborts.
This function mutates the array.
// Declare an array of integers. let numbers = [42, 23] // Remove the last element of the array. let twentyThree = numbers.removeLast() // `numbers` is now `[42]` // `twentyThree` is `23` // Remove the last element of the array. let fortyTwo = numbers.removeLast() // `numbers` is now `[]` // `fortyTwo` is `42` // Run-time error: The array is empty, the program aborts. numbers.removeLast()
Dictionaries
Dictionaries are mutable, unordered collections of key-value associations. Dictionaries may contain a key only once and may contain a value multiple times.
Dictionary literals start with an opening brace {
and end with a closing brace }.
Keys are separated from values by a colon,
and key-value associations are separated by commas.
// An empty dictionary
//
{}
// A dictionary which associates integers with booleans
//
{
1: true,
2: false
}Dictionary Types
Dictionary types have the form {K: V},
where K is the type of the key,
and V is the type of the value.
For example, a dictionary with Int keys and Bool
values has type {Int: Bool}.
In a dictionary, all keys must have a type that is a subtype of the dictionary’s key type (K)
and all values must have a type that is a subtype of the dictionary’s value type (V).
// Declare a constant that has type `{Int: Bool}`,
// a dictionary mapping integers to booleans.
//
let booleans = {
1: true,
0: false
}
// Declare a constant that has type `{Bool: Int}`,
// a dictionary mapping booleans to integers.
//
let integers = {
true: 1,
false: 0
}
// Mixing keys with different types, and mixing values with different types,
// is possible by declaring the expected dictionary type with the common supertype
// of all keys, and the common supertype of all values.
//
let mixedValues: {String: AnyStruct} = {
"a": 1,
"b": true
}Dictionary types are covariant in their key and value types.
For example, {Int: String} is a subtype of {AnyStruct: String}
and also a subtype of {Int: AnyStruct}.
This is safe because dictionaries are value types and not reference types.
Dictionary Access
To get the value for a specific key from a dictionary,
the access syntax can be used:
The dictionary is followed by an opening square bracket [, the key,
and ends with a closing square bracket ].
Accessing a key returns an optional:
If the key is found in the dictionary, the value for the given key is returned,
and if the key is not found, nil is returned.
// Declare a constant that has type `{Int: Bool}`,
// a dictionary mapping integers to booleans.
//
let booleans = {
1: true,
0: false
}
// The result of accessing a key has type `Bool?`.
//
booleans[1] // is `true`
booleans[0] // is `false`
booleans[2] // is `nil`
// Invalid: Accessing a key which does not have type `Int`.
//
booleans["1"]// Declare a constant that has type `{Bool: Int}`,
// a dictionary mapping booleans to integers.
//
let integers = {
true: 1,
false: 0
}
// The result of accessing a key has type `Int?`
//
integers[true] // is `1`
integers[false] // is `0`To set the value for a key of a dictionary, the access syntax can be used as well.
// Declare a constant that has type `{Int: Bool}`,
// a dictionary mapping booleans to integers.
//
let booleans = {
1: true,
0: false
}
// Assign new values for the keys `1` and `0`.
//
booleans[1] = false
booleans[0] = true
// `booleans` is `{1: false, 0: true}`Dictionary Fields and Functions
-
let length: IntThe number of entries in the dictionary.
// Declare a dictionary mapping strings to integers. let numbers = {"fortyTwo": 42, "twentyThree": 23} // Find the number of entries of the dictionary. let length = numbers.length // `length` is `2` -
access(Mutate | Insert) fun insert(key: K, _ value: V): V?Inserts the given value of type
Vinto the dictionary under the givenkeyof typeK.The inserted key must have the same type as the dictionary’s key type, and the inserted value must have the same type as the dictionary’s value type.
Returns the previous value as an optional if the dictionary contained the key, otherwise
nil.Updates the value if the dictionary already contained the key.
This function mutates the dictionary.
// Declare a dictionary mapping strings to integers. let numbers = {"twentyThree": 23} // Insert the key `"fortyTwo"` with the value `42` into the dictionary. // The key did not previously exist in the dictionary, // so the result is `nil` // let old = numbers.insert(key: "fortyTwo", 42) // `old` is `nil` // `numbers` is `{"twentyThree": 23, "fortyTwo": 42}` -
access(Mutate | Remove) fun remove(key: K): V?Removes the value for the given
keyof typeKfrom the dictionary.Returns the value of type
Vas an optional if the dictionary contained the key, otherwisenil.This function mutates the dictionary.
// Declare a dictionary mapping strings to integers. let numbers = {"fortyTwo": 42, "twentyThree": 23} // Remove the key `"fortyTwo"` from the dictionary. // The key exists in the dictionary, // so the value associated with the key is returned. // let fortyTwo = numbers.remove(key: "fortyTwo") // `fortyTwo` is `42` // `numbers` is `{"twentyThree": 23}` // Remove the key `"oneHundred"` from the dictionary. // The key does not exist in the dictionary, so `nil` is returned. // let oneHundred = numbers.remove(key: "oneHundred") // `oneHundred` is `nil` // `numbers` is `{"twentyThree": 23}` -
let keys: [K]Returns an array of the keys of type
Kin the dictionary. This does not modify the dictionary, just returns a copy of the keys as an array. If the dictionary is empty, this returns an empty array. The ordering of the keys is undefined.// Declare a dictionary mapping strings to integers. let numbers = {"fortyTwo": 42, "twentyThree": 23} // Find the keys of the dictionary. let keys = numbers.keys // `keys` has type `[String]` and is `["fortyTwo","twentyThree"]` -
let values: [V]Returns an array of the values of type
Vin the dictionary. This does not modify the dictionary, just returns a copy of the values as an array. If the dictionary is empty, this returns an empty array.This field is not available if
Vis a resource type.// Declare a dictionary mapping strings to integers. let numbers = {"fortyTwo": 42, "twentyThree": 23} // Find the values of the dictionary. let values = numbers.values // `values` has type [Int] and is `[42, 23]` -
access(all) view fun containsKey(key: K): BoolReturns true if the given key of type
Kis in the dictionary.// Declare a dictionary mapping strings to integers. let numbers = {"fortyTwo": 42, "twentyThree": 23} // Check if the dictionary contains the key "twentyFive". let containsKeyTwentyFive = numbers.containsKey("twentyFive") // `containsKeyTwentyFive` is `false` // Check if the dictionary contains the key "fortyTwo". let containsKeyFortyTwo = numbers.containsKey("fortyTwo") // `containsKeyFortyTwo` is `true` // Invalid: Check if the dictionary contains the key 42. // This results in a type error, as the key type of the dictionary is `String`. // let containsKey42 = numbers.containsKey(42) -
access(all) fun forEachKey(_ function: fun(K): Bool): VoidIterate through all the keys in the dictionary, exiting early if the passed function returns false. This is more efficient than calling
.keysand iterating over the resulting array, since an intermediate allocation is avoided. The order of key iteration is undefined, similar to.keys.// Take in a targetKey to look for, and a dictionary to iterate through. fun myContainsKey(targetKey: String, dictionary: {String: Int}) { // Declare an accumulator that we'll capture inside a closure. var found = false // At each step, `key` will be bound to another key from `dictionary`. dictionary.forEachKey(fun (key: String): Bool { found = key == targetKey // The returned boolean value, signals whether to continue iterating. // This allows for control flow during the iteration process: // true = `continue` // false = `break` return !found }) return found }
Dictionary Keys
Dictionary keys must be hashable and equatable.
Most of the built-in types, like booleans and integers, are hashable and equatable, so can be used as keys in dictionaries.
InclusiveRange
An InclusiveRange<T: Integer> value represents a range of numerical values between two integers,
with the start and end numbers included in the range as suggested by the name.
A range value has a start, end and a step, which represents the interval at which the range’s values are separated from start to end.
A range can be created using the InclusiveRange constructor, which can take two or three arguments.
In the case where the range is constructed with two arguments, the first argument is the start and the second is the end.
The step is inferred to be either 1 (when end >= start) or -1 (when end < start). E.g.
// start is 1, end is 100, step is 1
let range: InclusiveRange<UInt> = InclusiveRange(1, 100)Optionally a third, labeled, non-zero step argument can be provided to specify a step other than 1. E.g., the following range contains
all odd numbers from 1 to 100:
// start is 1, end is 100, step is 2
let range: InclusiveRange<UInt> = InclusiveRange(1, 100, step: 2)Note that in this example, even though the specified “end” of the range is 100, the last actual value the range can attain is 99.
If the specified step argument would progress the range away from the end, the creation will fail. E.g.
// Throws an error because a step of -2 cannot progress from 1 to 100
let range: InclusiveRange<Int> = InclusiveRange(1, 100, step: -2)A range requires a type annotation when created.
InclusiveRange<T> fields and functions
A value of type InclusiveRange<T>, where T is a number type, has the following fields and functions:
-
let start: TThe start of the range.
// Declare a range of `Int`s let range = let r: InclusiveRange<Int> = InclusiveRange(3, 9) // Get the start of the range let start = range.start // `start` is `3` -
let end: TThe end of the range.
// Declare a range of `Int`s let range: InclusiveRange<Int> = InclusiveRange(3, 9) // Get the end of the range let end = range.end // `end` is `9` -
let step: TThe step of the range.
// Declare a range of `Int`s with a `step` of 2 let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9, step: 2) // Get the step of the range var step = range.step // `step` is `2` // Declare a range of `Int`s without an explicit `step` let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9) / Get the step of the range step = rangeWithStep.step // `step` is implicitly `1` -
access(all) view fun contains(_ element: T): BoolReturns
trueif the given integer is in theInclusiveRangesequence, andfalseotherwise.Specifically, for some
InclusiveRangerdefined withstart,stepandend,r.contains(x)returns true if either:start <= endand there exists some integeri >= 0such thatstart + i * step = xandx <= endstart > endand there exists some integeri >= 0such thatstart - i * step = xandx >= end
// Declare a range of `Int`s with a `step` of 2 let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9, step: 2) // `contains` is `true` var contains = range.contains(5) // `contains` is `true` var contains = range.contains(9) // `contains` is `false` contains = range.contains(6) // `contains` is `false` contains = range.contains(11)