Skip to content

Commit

Permalink
Added examples of calling a transaction that takes an unowned paramet…
Browse files Browse the repository at this point in the history
…er and an explanation of how returns work.
  • Loading branch information
mcoblenz committed Sep 6, 2019
1 parent 983fb11 commit 212f2b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 11 additions & 4 deletions user_guide/source/tutorial/ownership2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ If ``>>`` is not specified for a certain parameter, then the ownership of that p

If a transaction expects an argument that is ``Unowned``, this means that the transaction cannot take ownership.
As a result, it is safe to pass an ``Owned`` reference as an argument to a transaction that expects an ``Unowned`` argument.
After the transaction returns, the caller still holds ownership.
After the transaction returns, the caller still holds ownership. For example:


For example, ``transaction bar(Money@Unowned m)`` can accept a
``Money`` reference with any ownership and the caller maintains whatever ownership it had initially when it called that transaction.
::

transaction logMoney(Money@Unowned m) {
...
}

transction callLogMoney(Money@Owned m) {
logMoney(m);
// OK; m is still an Owned reference.
}


Transaction receivers (``this``)
Expand Down
17 changes: 16 additions & 1 deletion user_guide/source/tutorial/ownership4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,19 @@ If ownership is no longer desired, ``disown`` can be used to relinquish ownershi
amount = amount + mergeFrom.amount;
disown mergeFrom; // We absorbed the value of mergeFrom, so the owner doesn't own it anymore.
}
}
}

Return
--------------
Return statements affect the type of the returned expression according to the declared return type. For example:

::

asset contract Candy {}

contract VendingMachine {
transaction dispenseCandy() returns Candy@Owned {
Candy c = ... // Get an owned reference somehow
return c; // Satisfies return type because ownership of c is transferred to the caller
// No accidental loss of c here because c is no longer Owned
}

0 comments on commit 212f2b3

Please sign in to comment.