Skip to content

Commit 5b953ce

Browse files
committed
main
1 parent 49a0ea8 commit 5b953ce

File tree

17 files changed

+398
-299
lines changed

17 files changed

+398
-299
lines changed

docs/document/Nix/docs/1. The Nix Language/Language Features/Assertion.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Language Features/Conditional Expression and Flow.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Language Features/Declaration and Expression.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Language Features/Logical Implication.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Language Features/With-Expression.md

Lines changed: 0 additions & 53 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Primitive Types/Function and Lambda.md

Lines changed: 0 additions & 75 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Primitive Types/Path Literal.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

docs/document/Nix/docs/1. The Nix Language/Primitive Types/String.md

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Control Flow
2+
3+
Nix is a functional language to behave as no side-effect, all variables are isolated in scope and all controls flow statement can have implicit return.
4+
5+
## Let-in
6+
7+
`let..in` statement is used to create a isolated scope to perform operation on variable declared after `let` and requires a return after `in`.
8+
9+
```nix
10+
let
11+
foo = 1;
12+
in
13+
foo # foo is returned
14+
```
15+
16+
Since it supports implicit return, you store the value to a variable.
17+
18+
```nix
19+
let
20+
foo = # [!code highlight]
21+
let # [!code highlight]
22+
bar = 123; # [!code highlight]
23+
in # [!code highlight]
24+
bar + 1; # [!code highlight]
25+
in
26+
foo
27+
```
28+
29+
> [!NOTE]
30+
> Child scope inside `let..in` statement can access variables from parent scope.
31+
32+
## Value Fallback
33+
34+
```nix
35+
foo or "foo"
36+
# equvalent to
37+
if foo != null then foo else "foo"
38+
```
39+
40+
## With Local Expanding
41+
42+
`with` works similarly to `let..in`, it creates a new scope and implicitly declares all attributes of a set as variables into the new scope.
43+
```nix
44+
let
45+
foo = {
46+
bar = 123;
47+
baz = 123;
48+
};
49+
in
50+
with foo;
51+
{
52+
inherit bar; # foo.bar and foo.baz is available in scope as variables # [!code highlight]
53+
inherit baz; # [!code highlight]
54+
}
55+
```
56+
57+
If a attribute expanded by `with` conflicts with any variable in scope by its name, **the variable is preferred**.
58+
59+
```nix
60+
let
61+
foo = {
62+
bar = 123;
63+
};
64+
bar = 456; # [!code highlight]
65+
in
66+
with foo;
67+
{
68+
inherit bar; # bar = 456 # [!code highlight]
69+
}
70+
```
71+
72+
## Assertion
73+
74+

0 commit comments

Comments
 (0)