Skip to main content
Version: Next

References

It is possible to create references to objects, i.e. resources or structures. A reference can be used to access fields and call functions on the referenced object.

References are copied, i.e. they are value types.

References have the type &T, where T is the type of the referenced object.

References are created using the & operator. The reference type must be explicitly provided, for example through a type annotation on a variable declaration, or a type assertion using the as operator.

let hello = "Hello"

// Create a reference to the `String` `hello`.
// Provide the reference type `&String` using a type assertion
//
let helloRef = &hello as &String

helloRef.length // is `5`

// Create another reference to the `String` `hello`.
// Provide the reference type `&String` using a type annotation instead
//
let alsoHelloRef: &String = &hello

// Invalid: Cannot create a reference without an explicit type
//
let unknownRef = &hello

The reference type must be a supertype of the referenced object's type.

// Invalid: Cannot create a reference to `hello`
// typed as `&Int`, as it has type `String`
//
let intRef = &hello as &Int

When creating a reference to an optional value, the result is an optional reference. If the referenced value is nil, the resulting reference itself will be nil. If the referenced value exists, then forcing the optional reference will yield a reference to that value:

let nilValue: String? = nil
let nilRef = &nilValue as &String? // r has type &String?
let n = nilRef! // error, forced nil value

let strValue: String? = ""
let strRef = &strValue as &String? // r has type &String?
let n = strRef! // n has type &String

References are covariant in their base types. For example, &T is a subtype of &U, if T is a subtype of U.


// Declare a resource interface named `HasCount`,
// that has a field `count`
//
resource interface HasCount {
count: Int
}

// Declare a resource named `Counter` that conforms to `HasCount`
//
resource Counter: HasCount {
pub var count: Int

pub init(count: Int) {
self.count = count
}

pub fun increment() {
self.count = self.count + 1
}
}

// Create a new instance of the resource type `Counter`
// and create a reference to it, typed as `&Counter`,
// so the reference allows access to all fields and functions
// of the counter
//
let counter <- create Counter(count: 42)
let counterRef: &Counter = &counter as &Counter

counterRef.count // is `42`

counterRef.increment()

counterRef.count // is `43`

References may be authorized or unauthorized.

Authorized references have the auth modifier, i.e. the full syntax is auth &T, whereas unauthorized references do not have a modifier.

Authorized references can be freely upcasted and downcasted, whereas unauthorized references can only be upcasted. Also, authorized references are subtypes of unauthorized references.


// Create an unauthorized reference to the counter,
// typed with the restricted type `&{HasCount}`,
// i.e. some resource that conforms to the `HasCount` interface
//
let countRef = &counter as &{HasCount}

countRef.count // is `43`

// Invalid: The function `increment` is not available
// for the type `&{HasCount}`
//
countRef.increment()

// Invalid: Cannot conditionally downcast to reference type `&Counter`,
// as the reference `countRef` is unauthorized.
//
// The counter value has type `Counter`, which is a subtype of `{HasCount}`,
// but as the reference is unauthorized, the cast is not allowed.
// It is not possible to "look under the covers"
//
let counterRef2: &Counter = countRef as? &Counter

// Create an authorized reference to the counter,
// again with the restricted type `{HasCount}`, i.e. some resource
// that conforms to the `HasCount` interface
//
let authCountRef = &counter as auth &{HasCount}

// Conditionally downcast to reference type `&Counter`.
// This is valid, because the reference `authCountRef` is authorized
//
let counterRef3: &Counter = authCountRef as? &Counter

counterRef3.count // is `43`

counterRef3.increment()

counterRef3.count // is `44`

References are ephemeral, i.e. they cannot be stored. Instead, consider storing a capability and borrowing it when needed.

Reference validity

Ephemeral references stay valid throughout the course of the program. However, references to resources can become invalid during the execution of a program, if the referenced resource is moved or destroyed after taking the reference.

let r <-create R()

// Take a reference to resource.
let ref = &r as &R

// Then transfer the resource into an account.
// This will invalidate all the references taken to the resource `r`.
account.save(<-r, to: /storage/r)

// Static error, since the referenced resource has been moved.
ref.id = 2

A reference is invalidated upon the first transfer of the underlying resource, regardless of the origin and the destination.

let ref = &r as &R

// Moving a resource to a different variable invalidates all references to it.
let r2 <- r

// Static error, since the referenced resource has been moved.
ref.id = 2
info

Invalidations of storage references are not statically caught, but only at run-time.