From 853252ac6699ec339f0692c913361b0df417ded6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 2 Oct 2006 16:14:30 +0000 Subject: * Document the new let. --- doc/manual/release-notes.xml | 3 ++ doc/manual/writing-nix-expressions.xml | 68 +++++++++++++--------------------- 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/doc/manual/release-notes.xml b/doc/manual/release-notes.xml index cc04a5d808bb..a4a4f5034407 100644 --- a/doc/manual/release-notes.xml +++ b/doc/manual/release-notes.xml @@ -118,6 +118,9 @@ irreversible. the availability of primop in a backwards-compatible way. + Real let-expressions: let x = ...; + ... z = ...; in .... + diff --git a/doc/manual/writing-nix-expressions.xml b/doc/manual/writing-nix-expressions.xml index ea719190f375..74ef3a868e77 100644 --- a/doc/manual/writing-nix-expressions.xml +++ b/doc/manual/writing-nix-expressions.xml @@ -716,36 +716,27 @@ encountered).. -Let expressions +Let-expressions -A let expression is a simple short-hand for a -rec expression followed by an attribute selection: -let { attrs } translates -to rec { attrs -}.body. - -For instance, - - -let { - x = "foo"; - y = "bar"; - body = x + y; -} - -is equivalent to +A let-expression allows you define local +variables for an expression. For instance, -rec { +let x = "foo"; y = "bar"; - body = x + y; -}.body +in x + y -and evaluates to "foobar". +evaluates to "foobar". +There is also an obsolete form of let-expression, +let { attrs }, which is +translated to rec { attrs +}.body. That is, the body of the let-expression is the +body attribute of the attribute set. + @@ -757,13 +748,13 @@ propagate attributes). This can be shortened using the inherit keyword. For instance, -let { +let x = 123; - body = { +in + { inherit x; y = 456; - }; -} + } evaluates to {x = 123; y = 456;}. (Note that this works because x is added to the lexical scope by @@ -819,10 +810,8 @@ function calls. a name, you can bind them to an attribute, e.g., -let { - concat = {x, y}: x + y; - body = concat {x = "foo"; y = "bar";}; -} +let concat = {x, y}: x + y; +in concat {x = "foo"; y = "bar";} @@ -837,11 +826,9 @@ where var is the name of the argument. It is not possible to define a default. Example: -let { - negate = x: !x; - concat = x: y: x + y; - body = if negate true then concat "foo" "bar" else ""; -} +let negate = x: !x; + concat = x: y: x + y; +in if negate true then concat "foo" "bar" else "" Note that concat is a function that takes one arguments and returns a function that takes another argument. This @@ -849,7 +836,7 @@ allows partial parameterisation (i.e., only filling some of the arguments of a function); e.g., - map (concat "foo") ["bar", "bla", "abc"] +map (concat "foo") ["bar", "bla", "abc"] evaluates to ["foobar" "foobla" "fooabc"]. @@ -958,9 +945,9 @@ used in the Nix expression for Subversion. -With expressions +With-expressions -A with expression, +A with-expression, with e1; e2 @@ -970,11 +957,8 @@ lexical scope of the expression e2. For instance, -let { - as = {x = "foo"; y = "bar";}; - - body = with as; x + y; -} +let as = {x = "foo"; y = "bar";}; +in with as; x + y evaluates to "foobar" since the with adds the x and -- cgit 1.4.1