documentation for 'var T' as return type

This commit is contained in:
Araq 2011-08-01 00:21:16 +02:00
commit dd99fe61c0
5 changed files with 52 additions and 23 deletions

View file

@ -2067,6 +2067,37 @@ One can use `tuple unpacking`:idx: to access the tuple's fields:
var (x, y) = divmod(8, 5) # tuple unpacking
assert x == 1
assert y == 3
Var return type
~~~~~~~~~~~~~~~
A proc, converter or iterator may return a ``var`` type which means that the
returned value is an l-value and can be modified by the caller:
.. code-block:: nimrod
var g = 0
proc WriteAccessToG(): var int =
result = g
WriteAccessToG() = 6
assert g == 6
It is a compile time error if the implicitely introduced pointer could be
used to access a location beyond its lifetime:
.. code-block:: nimrod
proc WriteAccessToG(): var int =
var g = 0
result = g # Error!
For iterators, a component of a tuple return type can have a ``var`` type too:
.. code-block:: nimrod
iterator modPairs(a: var seq[string]): tuple[key: int, val: var string] =
for i in 0..a.high:
yield (i, a[i])
Overloading of the subscript operator
@ -2340,7 +2371,7 @@ Type constraints
type parameter. Only the specified types are valid for instantiation:
.. code-block:: nimrod
proc onlyIntOrString[T: int|string](x, y: T): T = ...
proc onlyIntOrString[T: int|string](x, y: T): T = nil
onlyIntOrString(45, 66) # valid
onlyIntOrString(56.0, 0.0) # type mismatch