Merge pull request #2989 from apense/patch-11
Added example for var overloading
This commit is contained in:
commit
6f61264c49
1 changed files with 15 additions and 0 deletions
|
|
@ -294,6 +294,21 @@ If the formal parameter ``f`` is of type ``var T`` in addition to the ordinary
|
||||||
type checking, the argument is checked to be an `l-value`:idx:. ``var T``
|
type checking, the argument is checked to be an `l-value`:idx:. ``var T``
|
||||||
matches better than just ``T`` then.
|
matches better than just ``T`` then.
|
||||||
|
|
||||||
|
.. code-block:: nim
|
||||||
|
proc sayHi(x: int): string =
|
||||||
|
# matches a non-var int
|
||||||
|
result = $x
|
||||||
|
proc sayHi(x: var int): string =
|
||||||
|
# matches a var int
|
||||||
|
result = $(x + 10)
|
||||||
|
|
||||||
|
proc sayHello(x: int) =
|
||||||
|
var m = x # a mutable version of x
|
||||||
|
echo sayHi(x) # matches the non-var version of sayHi
|
||||||
|
echo sayHi(m) # matches the var version of sayHi
|
||||||
|
|
||||||
|
sayHello(3) # 3
|
||||||
|
# 13
|
||||||
|
|
||||||
Automatic dereferencing
|
Automatic dereferencing
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue