Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation around func and method #19207

Merged
merged 15 commits into from
Dec 4, 2021
30 changes: 28 additions & 2 deletions doc/tut1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
PMunch marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: nim
:test: "nim c $1"
Expand Down Expand Up @@ -874,6 +876,30 @@ 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,
PMunch marked this conversation as resolved.
Show resolved Hide resolved
functions, and methods, defined by the `proc`, `func`, and `method` keywords
respectively. In some ways Nim are a bit more pedantic in its definitions than
PMunch marked this conversation as resolved.
Show resolved Hide resolved
othe languages.
PMunch marked this conversation as resolved.
Show resolved Hide resolved

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 can't access global state (except `const`), can't
PMunch marked this conversation as resolved.
Show resolved Hide resolved
produce side-effects (they are tagged with `{.noSideEffects.}`). They can still
change their mutable arguments, which are those marked as `var` and any `ref`
objects.

Unlike procedures methods are dynamically dispatched. This sounds a bit
PMunch marked this conversation as resolved.
Show resolved Hide resolved
complicated but is a concept closely related to inheritance and object oriented
PMunch marked this conversation as resolved.
Show resolved Hide resolved
programming. If you overload a procedure (two procedures with the same name but
different types are said to be overloaded) the procedure to use is determined
PMunch marked this conversation as resolved.
Show resolved Hide resolved
at compile-time. Methods on the other hand depend on objects that inherit from
PMunch marked this conversation as resolved.
Show resolved Hide resolved
the `RootObj`. This is something that will be covered in much greater depth in
PMunch marked this conversation as resolved.
Show resolved Hide resolved
the `second part of the tutorial<tut2.html#object-oriented-programming-dynamic-dispatch>`_.


Iterators
=========

Expand Down