From c160b2161e1ec88677b8617420c49c78754c29f8 Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Tue, 25 Feb 2020 15:19:51 -0500 Subject: [PATCH] [backport] tut1: Update the proc overloading examples (#13497) [skip ci] (cherry picked from commit e4ed19c12fcce046d990a918800d9031706e8b66) --- doc/tut1.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 9b411c9325..6d54d88a38 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -743,13 +743,19 @@ Overloaded procedures Nim provides the ability to overload procedures similar to C++: .. code-block:: nim - proc toString(x: int): string = ... - proc toString(x: bool): string = - if x: result = "true" - else: result = "false" + proc toString(x: int): string = + result = + if x < 0: "negative" + elif x > 0: "positive" + else: "zero" - echo toString(13) # calls the toString(x: int) proc - echo toString(true) # calls the toString(x: bool) proc + proc toString(x: bool): string = + 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 `$ `_ operator in Nim.) The compiler chooses the most appropriate proc for the ``toString``