Cadence OOPs Mastery: References

References

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' type.

References may be authorized or unauthorized.

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

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