From defdd75beaa498ec636bfbf5bc7ae6a3c69b2f60 Mon Sep 17 00:00:00 2001 From: Peter Munch-Ellingsen Date: Thu, 2 Dec 2021 00:27:58 +0100 Subject: [PATCH 01/15] Improve documentation around func and method --- doc/tut1.rst | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index f492f12b04060..abd1590c90412 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -595,8 +595,10 @@ Procedures To define new commands like `echo `_ and `readLine `_ 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 `_. In +Nim new procedures are defined with the `proc` keyword: .. code-block:: nim :test: "nim c $1" @@ -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, +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 +othe 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 can't access global state (except `const`), can't +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 +complicated but is a concept closely related to inheritance and object oriented +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 +at compile-time. Methods on the other hand depend on objects that inherit from +the `RootObj`. This is something that will be covered in much greater depth in +the `second part of the tutorial`_. + + Iterators ========= From fc548e354e908d0341259dfc4040488ab142c08d Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 00:48:03 +0100 Subject: [PATCH 02/15] Update doc/tut1.rst Co-authored-by: Danil Yarantsev --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index abd1590c90412..1d18aba353adf 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -882,7 +882,7 @@ 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 are a bit more pedantic in its definitions than -othe languages. +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 From 0eb0453b7265f0109266fe638ab866e444216fe2 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 00:48:13 +0100 Subject: [PATCH 03/15] Update doc/tut1.rst Co-authored-by: Danil Yarantsev --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 1d18aba353adf..af21e53723b3b 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -881,7 +881,7 @@ 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 are a bit more pedantic in its definitions than +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 From 6aaccfb9deb808e6f409b570237231d1ee976d56 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:33:57 +0100 Subject: [PATCH 04/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index af21e53723b3b..21194d028dadc 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -598,7 +598,7 @@ and `readLine `_ in the examples, the concept of a *procedure* is needed. You might be used to them being called *methods* or *functions* in other languages, but Nim `differentiates these concepts `_. In -Nim new procedures are defined with the `proc` keyword: +Nim, new procedures are defined with the `proc` keyword: .. code-block:: nim :test: "nim c $1" From 6d6073b819f23159f95eed8637c8b7e0cd511d6b Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:34:04 +0100 Subject: [PATCH 05/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 21194d028dadc..e64334a43ff6e 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -879,7 +879,7 @@ whose value is then returned implicitly. Funcs and methods ----------------- -As mentioned in the introduction Nim differentiates between procedures, +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. From 82dcaa10ab4e9ad9a1e1c6d3d61125bf48a7f658 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:34:56 +0100 Subject: [PATCH 06/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index e64334a43ff6e..f08539020e584 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -891,7 +891,7 @@ 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 +Unlike procedures, methods are dynamically dispatched. This sounds a bit complicated but is a concept closely related to inheritance and object oriented 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 From 8826a448bcd556ab96cb7b6a7b32ba3bc0c6a8c2 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:35:05 +0100 Subject: [PATCH 07/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index f08539020e584..cb4456b564d36 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -892,7 +892,7 @@ change their mutable arguments, which are those marked as `var` and any `ref` objects. Unlike procedures, methods are dynamically dispatched. This sounds a bit -complicated but is a concept closely related to inheritance and object oriented +complicated, but is a concept closely related to inheritance and object oriented 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 at compile-time. Methods on the other hand depend on objects that inherit from From b5b79b94b0679b06deca73d6244e8d1a0cbd20f4 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:35:10 +0100 Subject: [PATCH 08/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index cb4456b564d36..088088f3dc01a 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -894,7 +894,7 @@ objects. Unlike procedures, methods are dynamically dispatched. This sounds a bit complicated, but is a concept closely related to inheritance and object oriented 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 +different types 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 will be covered in much greater depth in the `second part of the tutorial`_. From a46797281efe72fb201ebbfba06305e9fc66b01c Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 09:35:35 +0100 Subject: [PATCH 09/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 088088f3dc01a..793ab6e00c64f 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -886,7 +886,7 @@ 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 can't access global state (except `const`), can't +programming. Essentially they can't access global state (except `const`) and can't 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. From d1170f9e5942ac7d0b1459aac937d07600158fe7 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 11:52:01 +0100 Subject: [PATCH 10/15] Update doc/tut1.rst Co-authored-by: Zoom --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 793ab6e00c64f..0b737c0a6c749 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -892,7 +892,7 @@ change their mutable arguments, which are those marked as `var` and any `ref` objects. Unlike procedures, methods are dynamically dispatched. This sounds a bit -complicated, but is a concept closely related to inheritance and object oriented +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 different types 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 From 340ccae0d49d0d66abb549c30fa0ab5d8e411de5 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 11:52:09 +0100 Subject: [PATCH 11/15] Update doc/tut1.rst Co-authored-by: Zoom --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 0b737c0a6c749..3b943a04b6095 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -895,7 +895,7 @@ 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 different types 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 +at compile-time. Methods, on the other hand, depend on objects that inherit from the `RootObj`. This is something that will be covered in much greater depth in the `second part of the tutorial`_. From e3d75d521bd7a38ecadf3bd676d96d6dac24d9c1 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 11:52:40 +0100 Subject: [PATCH 12/15] Update doc/tut1.rst Co-authored-by: Zoom --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 3b943a04b6095..8003c8a8e3a70 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -894,7 +894,7 @@ 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 -different types are said to be overloaded), the procedure to use is determined +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 will be covered in much greater depth in the `second part of the tutorial`_. From 98c74d4f47ac2497d4dd10ce302cb36922049859 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 11:53:51 +0100 Subject: [PATCH 13/15] Update doc/tut1.rst Co-authored-by: Zoom --- doc/tut1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 8003c8a8e3a70..463a255cb4409 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -896,7 +896,7 @@ complicated, but it is a concept closely related to inheritance and object orien 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 will be covered in much greater depth in +the `RootObj`. This is something that is covered in much greater depth in the `second part of the tutorial`_. From dba8edceb552416229b874a3c355ab8698f0f839 Mon Sep 17 00:00:00 2001 From: Peter Munch-Ellingsen Date: Thu, 2 Dec 2021 12:00:28 +0100 Subject: [PATCH 14/15] Rewrite of Zooms suggestion --- doc/tut1.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 463a255cb4409..9adc9fea37022 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -886,10 +886,11 @@ 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 can't access global state (except `const`) and can't -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. +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 the `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 From 9dba150543d73636e7e7adadf4aad3ff5d907620 Mon Sep 17 00:00:00 2001 From: PMunch Date: Thu, 2 Dec 2021 12:21:45 +0100 Subject: [PATCH 15/15] Update doc/tut1.rst Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> --- doc/tut1.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/tut1.rst b/doc/tut1.rst index 9adc9fea37022..2856625b724d5 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -888,8 +888,8 @@ 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 the `proc` tagged -with `{.noSideEffects.}`). Functions can still change their mutable arguments +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