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:
_14var a = 1_14a = 2_14// `a` is `2`_14_14_14var b = 3_14var c = 4_14_14// Invalid: The assignment operation cannot be used in an expression._14a = b = c_14_14// Instead, the intended assignment must be written in multiple statements._14b = c_14a = b
Assignments to constants are invalid.
_10let a = 1_10// Invalid: Assignments are only for variables, not constants._10a = 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._10let numbers = [1, 2]_10_10// Change the first element of the array._10numbers[0] = 3_10_10// `numbers` is `[3, 2]`
_10// Declare an array of arrays of integers._10let arrays = [[1, 2], [3, 4]]_10_10// Change the first element in the second array_10arrays[1][0] = 5_10_10// `arrays` is `[[1, 2], [5, 4]]`
_11let dictionaries = {_11 true: {1: 2},_11 false: {3: 4}_11}_11_11dictionaries[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_29access(all)_29resource 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_29let a: @SomeResource <- create SomeResource(value: 5)_29_29// *Move* the resource value to a new constant._29_29let 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_29a.value // Error: a no longer exists_29_29// Constant `b` owns the resource._29_29b.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:
_14var a = 1_14var b = 2_14a <-> b_14// `a` is `2`_14// `b` is `1`_14_14var c = 3_14_14// Invalid: The swap operation cannot be used in an expression._14a <-> b <-> c_14_14// Instead, the intended swap must be written in multiple statements._14b <-> c_14a <-> b
Both sides of the swap operation must be variable, assignment to constants is invalid.
_10var a = 1_10let b = 2_10_10// Invalid: Swapping is only possible for variables, not constants._10a <-> b
Both sides of the swap operation must be an identifier, followed by one or more index or access expressions.