github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/gen/user-guide/scoping_doc.yaml (about) 1 - DocumentID: scoping 2 Title: >- 3 Variable and Config Scoping 4 CategoryID: user-guide 5 Summary: >- 6 How scoping works within Murex 7 Description: |- 8 ## Description 9 10 A 'scope' in Murex is a collection of code blocks to which variables and 11 config are persistent within. In Murex, a variable declared inside an `if` or 12 `foreach` block will be persistent outside of their blocks as long as you're 13 still inside the same function. 14 15 For example lets start with the following function that sets a variable called 16 **foo** 17 18 ``` 19 function example { 20 if { true } then { set foo=bar } 21 out $foo 22 } 23 ``` 24 25 In here the value is getting set inside an `if` block but its value is is 26 retrieved outside of that block. `out` and `set` have different parents but 27 the same scoping. 28 29 Then lets set **foo** outside of that function and see what happens: 30 31 ``` 32 » set foo=oof 33 » $foo 34 oof 35 36 » example 37 bar 38 39 » $foo 40 oof 41 ``` 42 43 Despite setting a variable named **foo**, the value inside **example** does not 44 overwrite the value outside **example** because they occupy different scoping. 45 46 ## What Instantiates A New Scope? 47 48 A new scope is instantiated by anything which resembles a function. This would 49 be code inside events, dynamic autocompletes, open agents, any code blocks 50 defined in `config`, as well as public and private functions too. 51 52 Code inside an `if`, `switch`, `foreach` and `source` do not create a new 53 scope. Subshells also do not create a new scoping either. 54 Synonyms: 55 Related: 56 - set 57 - let 58 - config 59 - reserved-vars 60 - function 61 - private 62 - openagent 63 - autocomplete 64 - event 65 - if 66 - switch 67 - foreach 68 - source 69