Tip: CTRL/⌘ + F and type in the symbol or operator you want to look up.
The &
(ampersand) symbol has several uses.
If an expression starts with the &
(ampersand) symbol, it creates a reference.
1let a: String = "hello"2let refOfA: &String = &a as &String
References may also be authorized if the &
symbol is preceded by auth
(otherwise the reference is unauthorized).
Authorized references have the auth
modifier, i.e. the full syntax is auth &T
,
whereas unauthorized references do not have a modifier.
1let a: String = "hello"2let refOfA: &String = &a as auth &String
It can be also used as a logical operator (AND),
by appearing twice in succession (i.e. &&
):
1let a = true2let b = false34let c = a && b // false
The @
(at) symbol before a type is used to annotate whether the type is a resource.
The @
symbol must appear at the beginning of the type, not inside.
For example, an array of NFT
s is @[NFT]
, not [@NFT]
.
This emphasizes the whole type acts like a resource.
1// Declare a resource named `SomeResource`2pub resource SomeResource {3pub var value: Int45init(value: Int) {6self.value = value7}8}910// we use the '@' symbol to reference a resource type11let a: @SomeResource <- create SomeResource(value: 0)1213// also in functions declarations14pub fun use(resource: @SomeResource) {15destroy resource16}
The :
(colon) symbol has several uses.
If a :
(colon) follows a variable/constant/function declaration, it is used to declare its type.
1let a: Bool = true // declares variable `a` with type `Bool`23// or45fun addOne(x: Int): Int { // return type of Int6return x + 17}
The :
(colon) is also be used in ternary operations to represent the "otherwise" section,
such as the following:
1let a = 1 > 2 ? 3 : 42// should be read as:3// "is 1 greater than 2?"4// "if YES, then set a = 3,5// "otherwise, set a = 4.
The =
(equals) symbol has several uses.
1let a = 1 // declares a variable `a` with value `1`
1a = 1 // assigns the value `1` to variable `a `
The !
(exclamation mark) symbol has a different effect whether it precedes or succeeds a variable.
When it immediately precedes a boolean-type variable, it negates it.
1let a: Bool = true2let b: Bool = !a34// b is false
When it immediately succeeds an optional variable, it force-unwraps it. Force-unwrapping returns the value inside an optional if it contains a value, or panics and aborts the execution if the optional has no value, i.e. the optional value is nil.
1let a: Int? = nil2let b: Int? = 334let c: Int = a! // panics, because = nil5let d: Int = b! // initialized correctly as 3
The /
(forward slash) symbol has several uses.
Inbetween two expressions, the forward slash acts as the division operator.
1let result = 4 / 2
In a Path, the forward slash separates the domain (e.g. storage
, private
, public
) and the identifier.
1let storagePath = /storage/path2storagePath.toString() // is "/storage/path"
The move operator <-
is like the assignment operator =
,
but must be used when the value is a resource.
To make assignment of resources explicit, the move operator <-
must be used when:
- The resource is the initial value of a constant or variable,
- The resource is moved to a different variable in an assignment,
- The resource is moved to a function as an argument
- The resource is returned from a function.
1resource R {}23let a <- create R() // we instantiate a new resource and move it into a
The force-assignment move operator <-!
moves a resource value to an optional variable.
If the variable is nil
, the move succeeds.
If it is not nil, the program aborts.
1pub resource R {}23var a: @R? <- nil4a <-! create R()
The swapping operator <->
swaps two resource between the variables to the left and right of it.
These are all typical arithmetic operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Remainder:
%
The ?
(question mark) symbol has several uses.
If a ?
(question mark) follows a variable/constant, it represents an optional.
An optional can either have a value or nothing at all.
1// Declare a constant which has an optional integer type2//3let a: Int? = nil
The ?
(question mark) is also be used in ternary operations to represent the "then" section,
such as the following:
1let a = 1 > 2 ? 3 : 42// should be read as:3// "is 1 greater than 2?"4// "if YES, then set a = 3,5// "otherwise, set a = 4.
The ?
(question mark) is also used in the nil-coalescing operator ??
.
It returns the value inside the optional, if the optional contains a value, or returns an alternative value if the optional has no value, i.e., the optional value is nil.
1// Declare a constant which has an optional integer type2//3let a: Int? = nil45// Declare a constant with a non-optional integer type,6// which is initialized to `a` if it is non-nil, or 42 otherwise.7//8let b: Int = a ?? 429// `b` is 42, as `a` is nil101112// Invalid: nil-coalescing operator is applied to a value which has a non-optional type13// (the integer literal is of type `Int`).14//15let c = 1 ?? 2
The _
(underscore) symbol has several uses.
The _
(underscore) can be used in names, e.g. in variables and types.
1let _a = true // used as a variable name2let another_one = false
The _
(underscore) can also be used to split up numerical components.
1let b = 100_000_000 // used to split up a number (supports all number types, e.g. 0b10_11_01)
The _
(underscore) can also be to indicate that a parameter in a function has no argument label.
1// The special argument label _ is specified for the parameter,2// so no argument label has to be provided in a function call.34fun double(_ x: Int): Int {5return x * 26}78let result = double(4)