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 are created by using the & operator, followed by the object, the as keyword, and the type through which they should be accessed. The given type must be a supertype of the referenced object's type.

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

1
let hello = "Hello"
2
3
// Create a reference to the "Hello" string, typed as a `String`
4
//
5
let helloRef: &String = &hello as &String
6
7
helloRef.length // is `5`
8
9
// Invalid: Cannot create a reference to `hello`
10
// typed as `&Int`, as it has type `String`
11
//
12
let intRef: &Int = &hello as &Int

If you attempt to reference an optional value, you will receive an optional reference. If the referenced value is nil, the reference itself will be nil. If the referenced value exists, then forcing the optional reference will yield a reference to that value:

1
let nilValue: String? = nil
2
let nilRef = &nilValue as &String? // r has type &String?
3
let n = nilRef! // error, forced nil value
4
5
let strValue: String? = ""
6
let strRef = &strValue as &String? // r has type &String?
7
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.

1
// Declare a resource interface named `HasCount`,
2
// that has a field `count`
3
//
4
resource interface HasCount {
5
count: Int
6
}
7
8
// Declare a resource named `Counter` that conforms to `HasCount`
9
//
10
resource Counter: HasCount {
11
pub var count: Int
12
13
pub init(count: Int) {
14
self.count = count
15
}
16
17
pub fun increment() {
18
self.count = self.count + 1
19
}
20
}
21
22
// Create a new instance of the resource type `Counter`
23
// and create a reference to it, typed as `&Counter`,
24
// so the reference allows access to all fields and functions
25
// of the counter
26
//
27
let counter <- create Counter(count: 42)
28
let counterRef: &Counter = &counter as &Counter
29
30
counterRef.count // is `42`
31
32
counterRef.increment()
33
34
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.

1
// Create an unauthorized reference to the counter,
2
// typed with the restricted type `&{HasCount}`,
3
// i.e. some resource that conforms to the `HasCount` interface
4
//
5
let countRef: &{HasCount} = &counter as &{HasCount}
6
7
countRef.count // is `43`
8
9
// Invalid: The function `increment` is not available
10
// for the type `&{HasCount}`
11
//
12
countRef.increment()
13
14
// Invalid: Cannot conditionally downcast to reference type `&Counter`,
15
// as the reference `countRef` is unauthorized.
16
//
17
// The counter value has type `Counter`, which is a subtype of `{HasCount}`,
18
// but as the reference is unauthorized, the cast is not allowed.
19
// It is not possible to "look under the covers"
20
//
21
let counterRef2: &Counter = countRef as? &Counter
22
23
// Create an authorized reference to the counter,
24
// again with the restricted type `{HasCount}`, i.e. some resource
25
// that conforms to the `HasCount` interface
26
//
27
let authCountRef: auth &{HasCount} = &counter as auth &{HasCount}
28
29
// Conditionally downcast to reference type `&Counter`.
30
// This is valid, because the reference `authCountRef` is authorized
31
//
32
let counterRef3: &Counter = authCountRef as? &Counter
33
34
counterRef3.count // is `43`
35
36
counterRef3.increment()
37
38
counterRef3.count // is `44`

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