diff options
author | PMunch <peterme@peterme.net> | 2021-12-04 07:54:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-04 07:54:11 +0100 |
commit | c658de24b04be9f898359a4091c5815f28de8785 (patch) | |
tree | 7bffe310fda8d1c1de58d756aceb260435cf94ad | |
parent | 1cbdc1573af5db8bf555b0b6454424188c9f345c (diff) | |
download | Nim-c658de24b04be9f898359a4091c5815f28de8785.tar.gz |
Improve documentation around func and method (#19207)
* Improve documentation around func and method * Update doc/tut1.rst Co-authored-by: Danil Yarantsev <tiberiumk12@gmail.com> * Update doc/tut1.rst Co-authored-by: Danil Yarantsev <tiberiumk12@gmail.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Update doc/tut1.rst Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com> * Rewrite of Zooms suggestion * Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Co-authored-by: Danil Yarantsev <tiberiumk12@gmail.com> Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Co-authored-by: Zoom <ZoomRmc@users.noreply.github.com>
-rw-r--r-- | doc/tut1.rst | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/doc/tut1.rst b/doc/tut1.rst index f492f12b0..2856625b7 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -595,8 +595,10 @@ Procedures To define new commands like `echo <system.html#echo,varargs[typed,]>`_ and `readLine <io.html#readLine,File>`_ in the examples, the concept of a -*procedure* is needed. (Some languages call them *methods* or *functions*.) -In Nim new procedures are defined with the `proc` keyword: +*procedure* is needed. You might be used to them being called *methods* or +*functions* in other languages, but Nim +`differentiates these concepts <tut1.html#procedures-funcs-and-methods>`_. In +Nim, new procedures are defined with the `proc` keyword: .. code-block:: nim :test: "nim c $1" @@ -874,6 +876,31 @@ The example also shows that a proc's body can consist of a single expression whose value is then returned implicitly. +Funcs and methods +----------------- + +As mentioned in the introduction, Nim differentiates between procedures, +functions, and methods, defined by the `proc`, `func`, and `method` keywords +respectively. In some ways, Nim is a bit more pedantic in its definitions than +other languages. + +Functions are closer to the concept of a pure mathematical +function, which might be familiar to you if you've ever done functional +programming. Essentially they are procedures with additional limitations set on +them: they can't access global state (except `const`) and can't produce +side-effects. The `func` keyword is basically an alias for `proc` tagged +with `{.noSideEffects.}`. Functions can still change their mutable arguments +however, which are those marked as `var`, along with any `ref` objects. + +Unlike procedures, methods are dynamically dispatched. This sounds a bit +complicated, but it is a concept closely related to inheritance and object oriented +programming. If you overload a procedure (two procedures with the same name but +of different types or with different sets of arguments are said to be overloaded), the procedure to use is determined +at compile-time. Methods, on the other hand, depend on objects that inherit from +the `RootObj`. This is something that is covered in much greater depth in +the `second part of the tutorial<tut2.html#object-oriented-programming-dynamic-dispatch>`_. + + Iterators ========= |