References

  1. struct Foo {}
  2. fn (foo Foo) bar_method() {
  3. // ...
  4. }
  5. fn bar_function(foo Foo) {
  6. // ...
  7. }

If a function argument is immutable (like foo in the examples above) V can pass it either by value or by reference. The compiler will decide, and the developer doesn’t need to think about it.

You no longer need to remember whether you should pass the struct by value or by reference.

You can ensure that the struct is always passed by reference by adding &:

  1. struct Foo {
  2. abc int
  3. }
  4. fn (foo &Foo) bar() {
  5. println(foo.abc)
  6. }

foo is still immutable and can’t be changed. For that, (mut foo Foo) must be used.

In general, V’s references are similar to Go pointers and C++ references. For example, a generic tree structure definition would look like this:

  1. struct Node<T> {
  2. val T
  3. left &Node
  4. right &Node
  5. }