Skip to content

Set

Name - Description Default Type
<input> Input becomes the value of the variable being created. Any
<output> The input to this shard is passed through as its output. Any
Name The name of the variable. "" String&Any
Key The key of the value to write in the table (parameter applicable only if the target variable is a table). None String&String
Global If the variable is available to all of the wires in the same mesh. false Bool
Exposed If the variable should be marked as exposed. false Bool

Creates a mutable variable and assigns a value to it.

Details

Set creates a mutable variable and assigns a value to it. Once created this variable can be modified.

The name of the variable comes from the :Name parameter and the variable value comes from the input. The type of input controls the kind of variable that will created: numeric input creates numeric variable, string input creates string variable, and sequence input would create a sequence variable.

To create a table variable, along with the input, you also have to pass the key in the :Key parameter. In this case the input (whatever it may be - numeric, string, sequence) becomes the value of the key that was passed in parameter :Key.

The :Global parameter controls whether the created variables can be referenced across wires (:Global set to true) or only within the current wire (:Global set to false, default behaviour).

Though it will generate a warning Set can also be used to update existing variables (like adding a new key-value pair to an existing table).

Note

Do not use Push to update any variables created by Set (or its aliases >=/>>=). Such variables are best best updated by Set itself (all types of variables) or AppendTo (only sequences and string variables).

Variables may be locally scoped (created with (:Global false); exists only for current wire) or globally scoped (created with (:Global true); exists for all wires of that mesh). Hence, in update mode (i.e. when you apply Set to an existing variable) the :Global parameter is used in conjunction with the :Name parameter to identify the correct variable to update.

The input to this shard is used as the value for the variable being created and is also passed through as this shard's output.

Note

Set has two aliases: >= is an alias for (Set ... :Global false) while >== is an alias for (Set ... :Global true). See the code examples at the end to understand how these aliases are used.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;; create a mutable string variable, modify it (by default available only to current wire)
"Hello" (Set :Name .svar)
.svar (Log ".svar")                 ;; => .svar: Hello
"World" (Update .svar)              ;; modify string variable
.svar (Log "modified .svar")        ;; => .svar: World

;; create a mutable numeric variable, modify it (available to all wires because `:Global true`)
100 (Set :Name .nvar :Global true)
.nvar (Log ".nvar")                 ;; => .nvar: 100
200 (Update .nvar)                  ;; modify numeric variable
.nvar (Log "modified .nvar")        ;; => modified .nvar: 200

;; create a mutable sequence
[10 20 30] (Set :Name .sequence)
.sequence (Log ".sequence")         ;; => .sequence: [10, 20, 30]

;; create a mutable table
(Const ["a" "b"]) (Set :Name "table" :Key "key1")
.table (Log ".table")               ;; => .table: {key1: [a, b]}

;; add a key-value pair to existing mutable table (will generate warnings)
"def" (Set "table" :Key "key2")     ;; add new key
.table (Log "modified .table")      ;; => modified .table: {key2: def, key1: [a, b]}

;; Using `Set` aliases

;; `>=` is alias for `(Set :Global false)`: create a mutable string variable
"World" >= .svarA
.svarA (Log ".svarA")               ;; => .svarA: World

;; `>==` is alias for `(Set :Global true)`: create a mutable numeric variable
100 >== .nvarA
.nvarA (Log ".nvarA")               ;; => .nvarA: 100

;; create and update local/global variables
"Local" (Set .str)                  ;; create local variable
"Global" (Set .str :Global true)    ;; create same-name global variable
(Get .str) (Log)                    ;; get updated local variable => "Local"
(Get .str :Global true) (Log)       ;; get same-name updated global variable => "Global"
"LocalNew" (Set .str)               ;; create local variable
"GlobalNew" (Set .str :Global true) ;; create same-name global variable
(Get .str) (Log)                    ;; get updated local variable => "LocalNew"
(Get .str :Global true) (Log)       ;; get same-name updated global variable => "GlobalNew"
[info] Set - Warning: setting an already exposed variable "str", use Update to avoid this warning.
[info] Set - Warning: setting an already exposed variable "str", use Update to avoid this warning.
[info] Set - Warning: setting an already exposed variable "str", use Update to avoid this warning.
[info] [shards/General/Set/Set.edn] .svar: Hello
[info] [shards/General/Set/Set.edn] modified .svar: World
[info] [shards/General/Set/Set.edn] .nvar: 100
[info] [shards/General/Set/Set.edn] modified .nvar: 200
[info] [shards/General/Set/Set.edn] .sequence: [10 20 30]
[info] [shards/General/Set/Set.edn] .table: {key1: [a b]}
[info] [shards/General/Set/Set.edn] modified .table: {key1: [a b] key2: def}
[info] [shards/General/Set/Set.edn] .svarA: World
[info] [shards/General/Set/Set.edn] .nvarA: 100
[info] [shards/General/Set/Set.edn] Local
[info] [shards/General/Set/Set.edn] Global
[info] [shards/General/Set/Set.edn] LocalNew
[info] [shards/General/Set/Set.edn] GlobalNew