-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
398 additions
and
299 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
docs/document/Nix/docs/1. The Nix Language/Language Features/Assertion.md
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
...x/docs/1. The Nix Language/Language Features/Conditional Expression and Flow.md
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
...nt/Nix/docs/1. The Nix Language/Language Features/Declaration and Expression.md
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
.../document/Nix/docs/1. The Nix Language/Language Features/Logical Implication.md
This file was deleted.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
docs/document/Nix/docs/1. The Nix Language/Language Features/With-Expression.md
This file was deleted.
Oops, something went wrong.
75 changes: 0 additions & 75 deletions
75
docs/document/Nix/docs/1. The Nix Language/Primitive Types/Function and Lambda.md
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
docs/document/Nix/docs/1. The Nix Language/Primitive Types/Path Literal.md
This file was deleted.
Oops, something went wrong.
65 changes: 0 additions & 65 deletions
65
docs/document/Nix/docs/1. The Nix Language/Primitive Types/String.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Control Flow | ||
|
||
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. | ||
|
||
## Let-in | ||
|
||
`let..in` statement is used to create a isolated scope to perform operation on variable declared after `let` and requires a return after `in`. | ||
|
||
```nix | ||
let | ||
foo = 1; | ||
in | ||
foo # foo is returned | ||
``` | ||
|
||
Since it supports implicit return, you store the value to a variable. | ||
|
||
```nix | ||
let | ||
foo = # [!code highlight] | ||
let # [!code highlight] | ||
bar = 123; # [!code highlight] | ||
in # [!code highlight] | ||
bar + 1; # [!code highlight] | ||
in | ||
foo | ||
``` | ||
|
||
> [!NOTE] | ||
> Child scope inside `let..in` statement can access variables from parent scope. | ||
## Value Fallback | ||
|
||
```nix | ||
foo or "foo" | ||
# equvalent to | ||
if foo != null then foo else "foo" | ||
``` | ||
|
||
## With Local Expanding | ||
|
||
`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. | ||
```nix | ||
let | ||
foo = { | ||
bar = 123; | ||
baz = 123; | ||
}; | ||
in | ||
with foo; | ||
{ | ||
inherit bar; # foo.bar and foo.baz is available in scope as variables # [!code highlight] | ||
inherit baz; # [!code highlight] | ||
} | ||
``` | ||
|
||
If a attribute expanded by `with` conflicts with any variable in scope by its name, **the variable is preferred**. | ||
|
||
```nix | ||
let | ||
foo = { | ||
bar = 123; | ||
}; | ||
bar = 456; # [!code highlight] | ||
in | ||
with foo; | ||
{ | ||
inherit bar; # bar = 456 # [!code highlight] | ||
} | ||
``` | ||
|
||
## Assertion | ||
|
||
|
Oops, something went wrong.