Skip to content

Commit

Permalink
doc: add a struct reference example (#13638)
Browse files Browse the repository at this point in the history
  • Loading branch information
kahsa committed Mar 3, 2022
1 parent ac1b31d commit 1e76ccc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/docs.md
Expand Up @@ -1929,6 +1929,32 @@ println(p.x)
The type of `p` is `&Point`. It's a [reference](#references) to `Point`.
References are similar to Go pointers and C++ references.

```v
struct Foo {
mut:
x int
}
fa := Foo{1}
mut a := fa
a.x = 2
assert fa.x == 1
assert a.x == 2
// fb := Foo{ 1 }
// mut b := &fb // error: `fb` is immutable, cannot have a mutable reference to it
// b.x = 2
mut fc := Foo{1}
mut c := &fc
c.x = 2
assert fc.x == 2
assert c.x == 2
println(fc) // Foo{ x: 2 }
println(c) // &Foo{ x: 2 } // Note `&` prefixed.
```
see also [Stack and Heap](#stack-and-heap)

### Default field values

```v
Expand Down

0 comments on commit 1e76ccc

Please sign in to comment.