From bd16f86c709e66b7c2696462acc4fbc7efefa493 Mon Sep 17 00:00:00 2001 From: s-ol Date: Fri, 21 Aug 2020 16:18:52 +0200 Subject: add reference/dynamic-symbols --- docs/reference/03-4_dynamic-symbols.md | 30 ++++++++++++++++++++++++++++++ docs/reference/index.md | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 docs/reference/03-4_dynamic-symbols.md (limited to 'docs/reference') 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 -- cgit v1.2.3