Nix (language): Difference between revisions

imported>Malteneuss
m small typo
imported>Malteneuss
m Improve with explanation
Line 250: Line 250:


==== <code>with</code> statement ====
==== <code>with</code> statement ====
The <code>with</code> statement introduces the lexical scope of a set into the expression which follows.
The <code>with</code> statement introduces the lexical scope of a set into the expression which follows. This means that it brings all keys within that set into scope in that expression. So, you don't need to use the dot notation.
Common usages are:
Common usages are:
'''On top of expressions''':
'''On top of expressions''':


Look at the following lib set:
<syntaxHighlight lang=nix>
lib = {
  ...
  types={
    attrsOf = ...;
    listOf = ...;
    str = ...;
  };
  ...
}
</syntaxHighlight>
You will see the with statement a lot at the beginning of expression definition. Most of the time it is used to load the lib functions into the namespace for quick access.
You will see the with statement a lot at the beginning of expression definition. Most of the time it is used to load the lib functions into the namespace for quick access.
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>