mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
* Document the new let.
This commit is contained in:
parent
ac19b333b3
commit
853252ac66
2 changed files with 29 additions and 42 deletions
|
@ -118,6 +118,9 @@ irreversible.</para></warning>
|
||||||
the availability of primop in a backwards-compatible
|
the availability of primop in a backwards-compatible
|
||||||
way.</para></listitem>
|
way.</para></listitem>
|
||||||
|
|
||||||
|
<listitem><para>Real let-expressions: <literal>let x = ...;
|
||||||
|
... z = ...; in ...</literal>.</para></listitem>
|
||||||
|
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
|
||||||
</para></listitem>
|
</para></listitem>
|
||||||
|
|
|
@ -716,36 +716,27 @@ encountered</quote>).</para></footnote>.</para>
|
||||||
</simplesect>
|
</simplesect>
|
||||||
|
|
||||||
|
|
||||||
<simplesect><title>Let expressions</title>
|
<simplesect><title>Let-expressions</title>
|
||||||
|
|
||||||
<para>A <literal>let</literal> expression is a simple short-hand for a
|
<para>A let-expression allows you define local
|
||||||
<literal>rec</literal> expression followed by an attribute selection:
|
variables for an expression. For instance,
|
||||||
<literal>let { <replaceable>attrs</replaceable> }</literal> translates
|
|
||||||
to <literal>rec { <replaceable>attrs</replaceable>
|
|
||||||
}.body</literal>.</para>
|
|
||||||
|
|
||||||
<para>For instance,
|
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
let {
|
let
|
||||||
x = "foo";
|
x = "foo";
|
||||||
y = "bar";
|
y = "bar";
|
||||||
body = x + y;
|
in x + y</programlisting>
|
||||||
}</programlisting>
|
|
||||||
|
|
||||||
is equivalent to
|
evaluates to <literal>"foobar"</literal>.
|
||||||
|
|
||||||
<programlisting>
|
|
||||||
rec {
|
|
||||||
x = "foo";
|
|
||||||
y = "bar";
|
|
||||||
body = x + y;
|
|
||||||
}.body</programlisting>
|
|
||||||
|
|
||||||
and evaluates to <literal>"foobar"</literal>.
|
|
||||||
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
<note><para>There is also an obsolete form of let-expression,
|
||||||
|
<literal>let { <replaceable>attrs</replaceable> }</literal>, which is
|
||||||
|
translated to <literal>rec { <replaceable>attrs</replaceable>
|
||||||
|
}.body</literal>. That is, the body of the let-expression is the
|
||||||
|
<literal>body</literal> attribute of the attribute set.</para></note>
|
||||||
|
|
||||||
</simplesect>
|
</simplesect>
|
||||||
|
|
||||||
|
|
||||||
|
@ -757,13 +748,13 @@ propagate attributes). This can be shortened using the
|
||||||
<literal>inherit</literal> keyword. For instance,
|
<literal>inherit</literal> keyword. For instance,
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
let {
|
let
|
||||||
x = 123;
|
x = 123;
|
||||||
body = {
|
in
|
||||||
|
{
|
||||||
inherit x;
|
inherit x;
|
||||||
y = 456;
|
y = 456;
|
||||||
};
|
}</programlisting>
|
||||||
}</programlisting>
|
|
||||||
|
|
||||||
evaluates to <literal>{x = 123; y = 456;}</literal>. (Note that this
|
evaluates to <literal>{x = 123; y = 456;}</literal>. (Note that this
|
||||||
works because <varname>x</varname> is added to the lexical scope by
|
works because <varname>x</varname> is added to the lexical scope by
|
||||||
|
@ -819,10 +810,8 @@ function calls.</para>
|
||||||
a name, you can bind them to an attribute, e.g.,
|
a name, you can bind them to an attribute, e.g.,
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
let {
|
let concat = {x, y}: x + y;
|
||||||
concat = {x, y}: x + y;
|
in concat {x = "foo"; y = "bar";}</programlisting>
|
||||||
body = concat {x = "foo"; y = "bar";};
|
|
||||||
}</programlisting>
|
|
||||||
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
@ -837,11 +826,9 @@ where <replaceable>var</replaceable> is the name of the argument. It
|
||||||
is not possible to define a default. Example:
|
is not possible to define a default. Example:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
let {
|
let negate = x: !x;
|
||||||
negate = x: !x;
|
concat = x: y: x + y;
|
||||||
concat = x: y: x + y;
|
in if negate true then concat "foo" "bar" else ""</programlisting>
|
||||||
body = if negate true then concat "foo" "bar" else "";
|
|
||||||
}</programlisting>
|
|
||||||
|
|
||||||
Note that <function>concat</function> is a function that takes one
|
Note that <function>concat</function> is a function that takes one
|
||||||
arguments and returns a function that takes another argument. This
|
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.,
|
arguments of a function); e.g.,
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
map (concat "foo") ["bar", "bla", "abc"]</programlisting>
|
map (concat "foo") ["bar", "bla", "abc"]</programlisting>
|
||||||
|
|
||||||
evaluates to <literal>["foobar" "foobla" "fooabc"]</literal>.</para>
|
evaluates to <literal>["foobar" "foobla" "fooabc"]</literal>.</para>
|
||||||
|
|
||||||
|
@ -958,9 +945,9 @@ used in the Nix expression for Subversion.</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<simplesect><title>With expressions</title>
|
<simplesect><title>With-expressions</title>
|
||||||
|
|
||||||
<para>A <emphasis>with</emphasis> expression,
|
<para>A <emphasis>with-expression</emphasis>,
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
with <replaceable>e1</replaceable>; <replaceable>e2</replaceable></programlisting>
|
with <replaceable>e1</replaceable>; <replaceable>e2</replaceable></programlisting>
|
||||||
|
@ -970,11 +957,8 @@ lexical scope of the expression <replaceable>e2</replaceable>. For
|
||||||
instance,
|
instance,
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
let {
|
let as = {x = "foo"; y = "bar";};
|
||||||
as = {x = "foo"; y = "bar";};
|
in with as; x + y</programlisting>
|
||||||
|
|
||||||
body = with as; x + y;
|
|
||||||
}</programlisting>
|
|
||||||
|
|
||||||
evaluates to <literal>"foobar"</literal> since the
|
evaluates to <literal>"foobar"</literal> since the
|
||||||
<literal>with</literal> adds the <varname>x</varname> and
|
<literal>with</literal> adds the <varname>x</varname> and
|
||||||
|
|
Loading…
Reference in a new issue