[backport] tut1: Update the proc overloading examples (#13497) [skip ci]
This commit is contained in:
parent
db540a0223
commit
e4ed19c12f
1 changed files with 12 additions and 6 deletions
18
doc/tut1.rst
18
doc/tut1.rst
|
|
@ -743,13 +743,19 @@ Overloaded procedures
|
||||||
Nim provides the ability to overload procedures similar to C++:
|
Nim provides the ability to overload procedures similar to C++:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
proc toString(x: int): string = ...
|
proc toString(x: int): string =
|
||||||
proc toString(x: bool): string =
|
result =
|
||||||
if x: result = "true"
|
if x < 0: "negative"
|
||||||
else: result = "false"
|
elif x > 0: "positive"
|
||||||
|
else: "zero"
|
||||||
|
|
||||||
echo toString(13) # calls the toString(x: int) proc
|
proc toString(x: bool): string =
|
||||||
echo toString(true) # calls the toString(x: bool) proc
|
result =
|
||||||
|
if x: "yep"
|
||||||
|
else: "nope"
|
||||||
|
|
||||||
|
assert toString(13) == "positive" # calls the toString(x: int) proc
|
||||||
|
assert toString(true) == "yep" # calls the toString(x: bool) proc
|
||||||
|
|
||||||
(Note that ``toString`` is usually the `$ <dollars.html>`_ operator in
|
(Note that ``toString`` is usually the `$ <dollars.html>`_ operator in
|
||||||
Nim.) The compiler chooses the most appropriate proc for the ``toString``
|
Nim.) The compiler chooses the most appropriate proc for the ``toString``
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue