Skip to main content
Version: Current

Glossary

info

Tip: CTRL/ + F and type in the symbol or operator you want to look up.

& (ampersand)

The & (ampersand) symbol has several uses.

Reference

If an expression starts with the & (ampersand) symbol, it creates a reference.


_10
let a: String = "hello"
_10
let 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.


_10
let a: String = "hello"
_10
let refOfA: &String = &a as auth &String

Logical Operator

It can be also used as a logical operator (AND), by appearing twice in succession (i.e. &&):


_10
let a = true
_10
let b = false
_10
_10
let c = a && b // false

@ (at)

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 NFTs is @[NFT], not [@NFT]. This emphasizes the whole type acts like a resource.


_16
// Declare a resource named `SomeResource`
_16
pub resource SomeResource {
_16
pub var value: Int
_16
_16
init(value: Int) {
_16
self.value = value
_16
}
_16
}
_16
_16
// we use the '@' symbol to reference a resource type
_16
let a: @SomeResource <- create SomeResource(value: 0)
_16
_16
// also in functions declarations
_16
pub fun use(resource: @SomeResource) {
_16
destroy resource
_16
}

: (colon)

The : (colon) symbol has several uses.

Type Declaration

If a : (colon) follows a variable/constant/function declaration, it is used to declare its type.


_10
let a: Bool = true // declares variable `a` with type `Bool`
_10
_10
// or
_10
_10
fun addOne(x: Int): Int { // return type of Int
_10
return x + 1
_10
}

Ternary Conditional Operator

The : (colon) is also be used in ternary operations to represent the "otherwise" section, such as the following:


_10
let a = 1 > 2 ? 3 : 4
_10
// should be read as:
_10
// "is 1 greater than 2?"
_10
// "if YES, then set a = 3,
_10
// "otherwise, set a = 4.

= (equals)

The = (equals) symbol has several uses.

Variable Declaration


_10
let a = 1 // declares a variable `a` with value `1`

Assignment


_10
a = 1 // assigns the value `1` to variable `a `

! (exclamation mark)

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.


_10
let a: Bool = true
_10
let b: Bool = !a
_10
_10
// 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.


_10
let a: Int? = nil
_10
let b: Int? = 3
_10
_10
let c: Int = a! // panics, because = nil
_10
let d: Int = b! // initialized correctly as 3

/ (forward slash)

The / (forward slash) symbol has several uses.

Division Operator

Inbetween two expressions, the forward slash acts as the division operator.


_10
let result = 4 / 2

Path separator

In a Path, the forward slash separates the domain (e.g. storage, private, public) and the identifier.


_10
let storagePath = /storage/path
_10
storagePath.toString() // is "/storage/path"

<- (lower than, hyphen) (Move operator)

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.

_10
resource R {}
_10
_10
let a <- create R() // we instantiate a new resource and move it into a

<-! (lower than, hyphen, exclamation mark) (Force-assignment move operator)

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.


_10
pub resource R {}
_10
_10
var a: @R? <- nil
_10
a <-! create R()

<-> (lower than, hyphen, greater than) (Swap operator)

The swapping operator <-> swaps two resource between the variables to the left and right of it.

+ (plus), - (minus), * (asterisk), % (percentage sign)

These are all typical arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Remainder: %

? (question mark)

The ? (question mark) symbol has several uses.

Optional

If a ? (question mark) follows a variable/constant, it represents an optional. An optional can either have a value or nothing at all.


_10
// Declare a constant which has an optional integer type
_10
//
_10
let a: Int? = nil

Ternary Conditional Operator

The ? (question mark) is also be used in ternary operations to represent the "then" section, such as the following:


_10
let a = 1 > 2 ? 3 : 4
_10
// should be read as:
_10
// "is 1 greater than 2?"
_10
// "if YES, then set a = 3,
_10
// "otherwise, set a = 4.

Nil-Coalescing Operator

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.


_15
// Declare a constant which has an optional integer type
_15
//
_15
let a: Int? = nil
_15
_15
// Declare a constant with a non-optional integer type,
_15
// which is initialized to `a` if it is non-nil, or 42 otherwise.
_15
//
_15
let b: Int = a ?? 42
_15
// `b` is 42, as `a` is nil
_15
_15
_15
// Invalid: nil-coalescing operator is applied to a value which has a non-optional type
_15
// (the integer literal is of type `Int`).
_15
//
_15
let c = 1 ?? 2

_ (underscore)

The _ (underscore) symbol has several uses.

Names

The _ (underscore) can be used in names, e.g. in variables and types.


_10
let _a = true // used as a variable name
_10
let another_one = false

Number Literals

The _ (underscore) can also be used to split up numerical components.


_10
let b = 100_000_000 // used to split up a number (supports all number types, e.g. 0b10_11_01)

Argument Labels

The _ (underscore) can also be to indicate that a parameter in a function has no argument label.


_10
// The special argument label _ is specified for the parameter,
_10
// so no argument label has to be provided in a function call.
_10
_10
fun double(_ x: Int): Int {
_10
return x * 2
_10
}
_10
_10
let result = double(4)