From 1af7e645ab164c669e19f9976b80a08a5f88b4b5 Mon Sep 17 00:00:00 2001 From: rayhem <49202382+rayhem@users.noreply.github.com> Date: Sat, 20 Jan 2024 22:25:50 -0500 Subject: [PATCH 1/3] Elaborate on `let` bindings The original documentation says let bindings are for "repeated use", though one can run ```nix a = 3; a + a ``` just fine without the binding. I think it's more appropriate to explain that `let` limits how far a value's name can reach. --- source/tutorials/nix-language.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/source/tutorials/nix-language.md b/source/tutorials/nix-language.md index 41cdf9c92..b8fd5c15c 100644 --- a/source/tutorials/nix-language.md +++ b/source/tutorials/nix-language.md @@ -371,7 +371,7 @@ error: undefined variable 'one' Also known as “`let` expression” or “`let` binding” -`let` expressions allow assigning names to values for repeated use. +`let` expressions allow assigning names to values for scoped use. Example: @@ -380,7 +380,7 @@ Example: let a = 1; in -a + a + a + a ``` ```{code-block} @@ -388,6 +388,24 @@ a + a 2 ``` +Attempting to use the value outside of the binding produces an error: + +```{code-block} nix +:class: expression +a +``` + +```{code-block} +:class: value +error: undefined variable 'a' + + at «string»:1:1: + + 1| a + | ^ +``` + + :::{dropdown} Detailed explanation Assignments are placed between the keywords `let` and `in`. From 7bfac9d9a99424109cd89c141e31d4c83a53f11a Mon Sep 17 00:00:00 2001 From: rayhem <49202382+rayhem@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:22:21 -0500 Subject: [PATCH 2/3] Update source/tutorials/nix-language.md Co-authored-by: Henrik --- source/tutorials/nix-language.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/tutorials/nix-language.md b/source/tutorials/nix-language.md index b8fd5c15c..14f568ad4 100644 --- a/source/tutorials/nix-language.md +++ b/source/tutorials/nix-language.md @@ -371,7 +371,7 @@ error: undefined variable 'one' Also known as “`let` expression” or “`let` binding” -`let` expressions allow assigning names to values for scoped use. +`let` expressions allow assigning names to values for repeated and scoped use. Example: From e315973005777ad7c643ffdf8d9a719988ea9ad4 Mon Sep 17 00:00:00 2001 From: rayhem <49202382+rayhem@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:24:07 -0500 Subject: [PATCH 3/3] Remove out-of-scope example --- source/tutorials/nix-language.md | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/source/tutorials/nix-language.md b/source/tutorials/nix-language.md index 14f568ad4..c94aec154 100644 --- a/source/tutorials/nix-language.md +++ b/source/tutorials/nix-language.md @@ -388,24 +388,6 @@ in 2 ``` -Attempting to use the value outside of the binding produces an error: - -```{code-block} nix -:class: expression -a -``` - -```{code-block} -:class: value -error: undefined variable 'a' - - at «string»:1:1: - - 1| a - | ^ -``` - - :::{dropdown} Detailed explanation Assignments are placed between the keywords `let` and `in`.