diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-08-21 14:18:52 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 38b8ce723bd66036837138770884db4cd7a75a18 (patch) | |
| tree | b6921b1f3352639d54cb682ebd4b6cd97851086b /docs/reference | |
| parent | add reference/conditionals (diff) | |
| download | alive-38b8ce723bd66036837138770884db4cd7a75a18.tar.gz alive-38b8ce723bd66036837138770884db4cd7a75a18.zip | |
add reference/dynamic-symbols
Diffstat (limited to 'docs/reference')
| -rw-r--r-- | docs/reference/03-4_dynamic-symbols.md | 30 | ||||
| -rw-r--r-- | docs/reference/index.md | 2 |
2 files changed, 31 insertions, 1 deletions
diff --git a/docs/reference/03-4_dynamic-symbols.md b/docs/reference/03-4_dynamic-symbols.md new file mode 100644 index 0000000..d642984 --- /dev/null +++ b/docs/reference/03-4_dynamic-symbols.md @@ -0,0 +1,30 @@ +Symbol definitions in `alv` normally follow 'lexical scoping' rules. That means +that symbols are looked up by following the scopes outwards according to the +syntactical nesting of expressions in the source code. + +In the following snippet, for example, the symbol `hello` is resolved inside +`print-hello` by checking first the innermost scope (the function body), and +then the surrounding scope (the whole file), where the value `"original +message"` is found: + + (def hello "original message") + (defn print-hello () (print hello)) + + (do + (def hello "overwritten message") + (print-hello)) #(-> prints "original message") + +On the other hand, there are also *dynamic symbols*. Dynamic symbols are +symbols whose name starts and ends with an asterisk, like `*clock*` and +`*sym*`. Where functions are called, dynamic symbols are not looked up in the +scope that contains the *function definition*, but rather the scope containing +the *function call site*: + + (def *hello* "original message") + (defn print-hello () (print *hello*)) + + (do + (def *hello* "overwritten message") + (print-hello)) #(-> prints "overwritten message") + +This allows symbols to be *dynamically overwritten*. diff --git a/docs/reference/index.md b/docs/reference/index.md index f0335c3..bf8e5f3 100644 --- a/docs/reference/index.md +++ b/docs/reference/index.md @@ -16,7 +16,7 @@ own module or contributing to alive, check out the 1. [symbol resolution](03-1_symbol-resolution.html) 2. [conditionals](03-2_conditionals.html) 3. [functions](03-3_functions.html) - 4. dynamic symbols + 4. [dynamic symbols](03-4_dynamic-symbols.html) 5. [loops](03-5_loops.html) 6. modules and loading 4. runtime |
