tazjin / nix-1p

A (more or less) one page introduction to Nix, the language.
https://code.tvl.fyi/about/nix/nix-1p
896 stars 24 forks source link

"Everything in Nix is an expression, meaning that every directive returns some kind of data." #19

Closed WayneSherman closed 7 months ago

WayneSherman commented 7 months ago

"Everything in Nix is an expression" There are parts of the language that are not expressions. For example, "Let" could be described as a "keyword" since it is a predefined reserved word in the Nix language. Although "Let" is "in Nix", it isn't an expression by itself. There are syntactical elements which are also part of Nix, but they are not expressions. For example "[" is a valid part of the Nix language, but not an expression by itself. In the Nix repl, if I type "Let" (and then enter) or "[" (and then enter) I get error messages.

"meaning that every directive returns some kind of data." What is a "directive"?

tazjin commented 7 months ago

let absolutely is an expression that returns a value! A let consists of the binding declarations, and the body (after in, when using modern Nix). Evaluating this returns a value (the body with the bindings in place). This is referentially transparent and there is nothing sequential going on, i.e. there are no statements.

A directive here is meant to be any well-formed snippet of Nix code. I think you're confusing the lexical level (which tokens exist) with the semantic level (what does the parser construct out of the tokens), and I'm not sure how to make that clearer.

WayneSherman commented 7 months ago

A suggestion to clarify this statement:

Everything in Nix is an expression meaning that every directive returns some kind of data.

Every well-formed (i.e. syntactically correct) block of code in Nix is an expression that returns a value.

("Well-formed" and "Syntactically correct" mean the block of code is syntactically complete and the structure, syntax, and order of elements adhere to the defined rules of the language.)

Alternatives to "block of code": "unit of code" "code segment"

tazjin commented 7 months ago

How about a slightly simpler

Any valid piece of Nix code is [...]

?

I think that "well-formed" != "syntactically correct", as you can write syntactically correct code that fails to evaluate (e.g. builtins.add {} {})

WayneSherman commented 7 months ago

How about a slightly simpler

Any valid piece of Nix code is [...]

Yes I think that is an improvement.

tazjin commented 7 months ago

Making the change here: https://cl.tvl.fyi/c/depot/+/10737