Skip to content

Commit c658de2

Browse files
PMunchDanil YarantsevkonsumlammZoomRmc
authored
Improve documentation around func and method (#19207)
* Improve documentation around func and method * Update doc/tut1.rst Co-authored-by: Danil Yarantsev <[email protected]> * Update doc/tut1.rst Co-authored-by: Danil Yarantsev <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> * Update doc/tut1.rst Co-authored-by: Zoom <[email protected]> * Update doc/tut1.rst Co-authored-by: Zoom <[email protected]> * Update doc/tut1.rst Co-authored-by: Zoom <[email protected]> * Update doc/tut1.rst Co-authored-by: Zoom <[email protected]> * Rewrite of Zooms suggestion * Update doc/tut1.rst Co-authored-by: konsumlamm <[email protected]> Co-authored-by: Danil Yarantsev <[email protected]> Co-authored-by: konsumlamm <[email protected]> Co-authored-by: Zoom <[email protected]>
1 parent 1cbdc15 commit c658de2

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

doc/tut1.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,10 @@ Procedures
595595

596596
To define new commands like `echo <system.html#echo,varargs[typed,]>`_
597597
and `readLine <io.html#readLine,File>`_ in the examples, the concept of a
598-
*procedure* is needed. (Some languages call them *methods* or *functions*.)
599-
In Nim new procedures are defined with the `proc` keyword:
598+
*procedure* is needed. You might be used to them being called *methods* or
599+
*functions* in other languages, but Nim
600+
`differentiates these concepts <tut1.html#procedures-funcs-and-methods>`_. In
601+
Nim, new procedures are defined with the `proc` keyword:
600602

601603
.. code-block:: nim
602604
:test: "nim c $1"
@@ -874,6 +876,31 @@ The example also shows that a proc's body can consist of a single expression
874876
whose value is then returned implicitly.
875877

876878

879+
Funcs and methods
880+
-----------------
881+
882+
As mentioned in the introduction, Nim differentiates between procedures,
883+
functions, and methods, defined by the `proc`, `func`, and `method` keywords
884+
respectively. In some ways, Nim is a bit more pedantic in its definitions than
885+
other languages.
886+
887+
Functions are closer to the concept of a pure mathematical
888+
function, which might be familiar to you if you've ever done functional
889+
programming. Essentially they are procedures with additional limitations set on
890+
them: they can't access global state (except `const`) and can't produce
891+
side-effects. The `func` keyword is basically an alias for `proc` tagged
892+
with `{.noSideEffects.}`. Functions can still change their mutable arguments
893+
however, which are those marked as `var`, along with any `ref` objects.
894+
895+
Unlike procedures, methods are dynamically dispatched. This sounds a bit
896+
complicated, but it is a concept closely related to inheritance and object oriented
897+
programming. If you overload a procedure (two procedures with the same name but
898+
of different types or with different sets of arguments are said to be overloaded), the procedure to use is determined
899+
at compile-time. Methods, on the other hand, depend on objects that inherit from
900+
the `RootObj`. This is something that is covered in much greater depth in
901+
the `second part of the tutorial<tut2.html#object-oriented-programming-dynamic-dispatch>`_.
902+
903+
877904
Iterators
878905
=========
879906

0 commit comments

Comments
 (0)