Skip to content

Variables

Dave DeLong edited this page Sep 18, 2015 · 1 revision

If you don't know what the value of a particular term should be when the string is constructed, that's ok; simply use a variable:

let math = "6 * $a"

When you figure out what the value is supposed to be, you can pass it along in the substitution dictionary:

let substitutions = ["a": 7.0]
let value = try math.evaluate(substitutions) // value is 42

Variables are denoted in the source string as beginning with $ and can contain numbers or letters. They can also be indicated by wrapping characters in single- or double quotes, like so:

let math = "6 * 'a'"

These "string" variables can also contain whitespace and escaped quote characters:

let math = "6 * 'Hello, \'World\''"

All variables are case sensitive. ($a is not the same as $A)