Skip to content

Ref

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 read from 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
Overwrite If the variable should be overwritten if it already exists. false Bool

Creates an immutable variable with a constant value. Once created this variable cannot be changed.

Details

Ref creates an immutable variable and assigns a constant value to it. Once created this variable cannot be changed.

The name of the variable comes from the :Name parameter and the constant 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).

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

Note

Ref has two aliases: = and &>. Both are aliases for (Ref ... :Global false). See the code examples at the end to understand how these aliases are used.

See also

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
;; create an immutable string variable (by default available only to current wire)
"Hello" (Ref :Name .svar)
.svar (Log ".svar")                 ;; => .svar: Hello

;; uncomment next line to see the immutable-variable-update error
;; 20 (Update .svar)
;; => Error composing shard: Set/Ref/Update, attempted to write an immutable variable.

;; create an immutable numeric variable (available to all wires because `:Global true`)
100 (Ref :Name .nvar :Global true)
.nvar (Log ".nvar")                 ;; => .nvar: 100

;; create an immutable sequence
[10 20] (Ref :Name .seq)
.seq (Log ".seq")                   ;; => .seq: [10, 20]

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

;; Using `Ref` aliases

;; `=` is alias for `(Ref :Global false)`: create an immutable string variable: 
"World" = .svarA
.svarA (Log ".svarA")               ;; => .svarA: World

;; `&>` is another alias for `(Ref :Global false)`: create an immubtable numeric variable
100 &> .nvarA
.nvarA (Log ".nvarA")               ;; => .nvarA: 100
[info] [shards/General/Ref/Ref.edn] .svar: Hello
[info] [shards/General/Ref/Ref.edn] .nvar: 100
[info] [shards/General/Ref/Ref.edn] .seq: [10 20]
[info] [shards/General/Ref/Ref.edn] .table: {key1: [a b]}
[info] [shards/General/Ref/Ref.edn] .svarA: World
[info] [shards/General/Ref/Ref.edn] .nvarA: 100