Skip to content

Commit

Permalink
Elaborate on declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
masak committed May 21, 2024
1 parent d81bb0d commit 7010f9d
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions spec/04-declarations.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
# Chapter 4: Declarations

Declarations establish a binding between a name and a value, or sometimes
several such name/value bindings. Unlike expressions whose primary role is to
be evaluated, and statements which are primarily executed, declarations add
their binding to the lexical environment.
Declarations establish a binding between a name and a value. Unlike expressions
whose primary role is to be evaluated, and statements which are primarily
executed, declarations add their binding to the lexical environment.

A declaration belongs to its innermost enclosing block. As a block is run,
first a new frame is created, empty but linked to the frame of the innermost
surrounding block, if any. All the declarations belonging to the block get a
binding with their name, to a pseudo-value called `uninitialized`. After that,
declarations initialize on two different schedules, depending on their type.

_Hoisted declarations_ initialize immediately, meaning that their binding will
be initialized by the time the first statement is executed. Hoisted
declarations of the same block are free to mutually refer to each other, since
by design their initialization doesn't evaluate any expressions or execute any
statements; when they do refer to each other, they do so in code which is not
yet running. The specific initialization order of hoisted declarations in a
block is unobservable; any order produces the same end result.

_Inline declarations_ (only variable declarations in the base language)
have an expression called an _initialized expression_, which is evaluated the
moment execution reaches the declaration. The resulting value is then used to
initialize the declared name. Reading a variable before it has been initialized
(which includes in the initializer expression itself) results in a runtime
error.

Many declaration forms are _complex_ and contain nested declarations. These
nested declarations are typically for a different scope. For example, function
declarations contain parameters, which declare names to be used in the function
body. Classes declare fields or methods, which declare names to be used in
connection with instances of the class.

```
<declaration> ::= <variable-declaration>
Expand Down

0 comments on commit 7010f9d

Please sign in to comment.