Skip to main content

Assignment, Move, Force-Assignment, and Swapping Operators

Assignment operator (=)

The binary assignment operator = can be used to assign a new value to a variable. It is only allowed in a statement and is not allowed in expressions:


_14
var a = 1
_14
a = 2
_14
// `a` is `2`
_14
_14
_14
var b = 3
_14
var c = 4
_14
_14
// Invalid: The assignment operation cannot be used in an expression.
_14
a = b = c
_14
_14
// Instead, the intended assignment must be written in multiple statements.
_14
b = c
_14
a = b

Assignments to constants are invalid.


_10
let a = 1
_10
// Invalid: Assignments are only for variables, not constants.
_10
a = 2

The left-hand side of the assignment operand must be an identifier. For arrays and dictionaries, this identifier can be followed by one or more index or access expressions.


_10
// Declare an array of integers.
_10
let numbers = [1, 2]
_10
_10
// Change the first element of the array.
_10
numbers[0] = 3
_10
_10
// `numbers` is `[3, 2]`


_10
// Declare an array of arrays of integers.
_10
let arrays = [[1, 2], [3, 4]]
_10
_10
// Change the first element in the second array
_10
arrays[1][0] = 5
_10
_10
// `arrays` is `[[1, 2], [5, 4]]`


_11
let dictionaries = {
_11
true: {1: 2},
_11
false: {3: 4}
_11
}
_11
_11
dictionaries[false][3] = 0
_11
_11
// `dictionaries` is `{
_11
// true: {1: 2},
_11
// false: {3: 0}
_11
//}`

Move operator (<-)

The move operator (<-) is unique to Cadence and is used to move resource types from one location to another. It works similar to the assignment operator (=) you're used to from most programming languages, except that the data in the location on the right side of the statement is destroyed by the operation:


_29
// Declare a resource named `SomeResource`, with a variable integer field.
_29
_29
access(all)
_29
resource SomeResource {
_29
_29
access(all)
_29
var value: Int
_29
_29
init(value: Int) {
_29
self.value = value
_29
}
_29
}
_29
_29
// Declare a constant with value of resource type `SomeResource`.
_29
_29
let a: @SomeResource <- create SomeResource(value: 5)
_29
_29
// *Move* the resource value to a new constant.
_29
_29
let b <- a
_29
_29
// Invalid Line Below: Cannot use constant `a` anymore as the resource that it
_29
// referred to was moved to constant `b`.
_29
_29
a.value // Error: a no longer exists
_29
_29
// Constant `b` owns the resource.
_29
_29
b.value // equals 5

Force-assignment operator (<-!)

The force-assignment operator (<-!) assigns a resource-typed value to an optional-typed variable if the variable is nil. If the variable being assigned to is non-nil, the execution of the program aborts.

The force-assignment operator is only used for resource types.

Swapping operator (<->)

The binary swap operator <-> can be used to exchange the values of two variables. It is only allowed in a statement and is not allowed in expressions:


_14
var a = 1
_14
var b = 2
_14
a <-> b
_14
// `a` is `2`
_14
// `b` is `1`
_14
_14
var c = 3
_14
_14
// Invalid: The swap operation cannot be used in an expression.
_14
a <-> b <-> c
_14
_14
// Instead, the intended swap must be written in multiple statements.
_14
b <-> c
_14
a <-> b

Both sides of the swap operation must be variable, assignment to constants is invalid.


_10
var a = 1
_10
let b = 2
_10
_10
// Invalid: Swapping is only possible for variables, not constants.
_10
a <-> b

Both sides of the swap operation must be an identifier, followed by one or more index or access expressions.