From 42a484de3e937efd9b177454950332aeb03c1215 Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Mon, 8 Apr 2024 14:19:39 -0400 Subject: [PATCH] docs for dereference --- .../version-1.0/language/references.mdx | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/versioned_docs/version-1.0/language/references.mdx b/versioned_docs/version-1.0/language/references.mdx index 4ee753f..030b869 100644 --- a/versioned_docs/version-1.0/language/references.mdx +++ b/versioned_docs/version-1.0/language/references.mdx @@ -417,3 +417,29 @@ ref.id = 2 Invalidations of storage references are not statically caught, but only at run-time. + +## Dereferencing values + +Primitive values (and arrays or dictionaries of primitive values) can be "de-referenced" using the unary `*` operator. +This operation produces a copy of the referenced value, so e.g. given some example code: + +```cadence +var x = 3 +let ref: &Int = &x +var y = *ref +y = y + 1 +``` + +At the end of the execution of this code, `y` will clearly be `4`, but `x` will still be `3`, as the `*ref` operation copies +the value. This can be seen even more clearly using an example with arrays: + +```cadence +let x = [0] +let ref: &[Int] = &x +var y = *ref +y.append(1) +``` + +At the end of this execution, `y` will contain `[0, 1]`, while `x` will remain `[0]` only. + +References to non-primitive values (e.g. structs, resources, contracts and enums) cannot be dereferenced. \ No newline at end of file