Variables
In R, a variable comes into existence the first time you assign to it. In Vapour, you declare it first, with a type. This one rule lets Vapour catch typos, accidental overwrites, and values of the wrong type.
Declaring with let
Declare a variable with let, its name, a colon, its type, and
optionally a value.
- Vapour
- R
let name: char = "Ada"
let age: int = 36
name = "Ada"
age = 36
The type always comes after a colon. The types chapter covers all of them, for now you only need a few:
| Type | Holds | Example |
|---|---|---|
char | text | "hello" |
int | whole numbers | 42 |
num | any number | 3.14 |
bool | true or false | true |
You can declare a variable without a value and assign it later.
let total: int
total = 10
Assigning with =
Once declared, assign to a variable with =.
The new value must match the declared type.
let age: int = 36
age = 37
# error: left expects `int`, right returns `char`
age = "thirty-seven"
In Vapour, <- does not mean ordinary assignment, it assigns to a
variable in a parent scope, see below.
Always use =.
To add to or subtract from a variable, use += and -=.
- Vapour
- R
let count: int = 40
count += 2
count -= 1
count = 40
count = count + 2
count = count - 1
Declare once
A variable can only be declared once in the same scope. This protects you from accidentally reusing a name you already had.
let x: int = 1
# error: variable `x` is already declared
let x: char = "a"
Inside a function you are in a new scope, so you may declare a variable with the same name as one outside of it. The inner one hides (shadows) the outer one until the function ends.
let x: int = 1
func f(): char {
let x: char = "inside"
return x
}
Constants with const
Use const for a value that should never change.
A constant must have a value when declared.
- Vapour
- R
const max_retries: int = 3
# error: `max_retries` is a constant
max_retries = 5
max_retries = 3
Two things to know about constants:
- They are only checked by Vapour. In the R output a constant is an ordinary variable, nothing locks it at run time.
- Only the name is constant. If the constant holds an object, you can still change its attributes.
Prefer const whenever a value doesn't need to change: it tells the
reader that it won't.
Assigning to a parent variable
Sometimes a function needs to update a variable declared outside of it.
In R you use <<-, in Vapour you use <-.
- Vapour
- R
let calls: int = 0
func track(): null {
calls <- calls + 1
}
track()
track()
print(calls) # 2
calls = 0
track = function() {
calls <<- calls + 1
}
track()
track()
print(calls) # 2
Vapour checks that the variable you assign to with <- exists.
In R, <<- silently creates a global variable when the name is
misspelled, in Vapour it's an error.
func reset(): null {
# error: `totl` does not exist in parent environment
totl <- 0
}
Values
Most values are written as in R.
Numbers
Whole numbers are int, numbers with a decimal point are num.
let n: int = 42
let big: int = 1e3
let pi_ish: num = 3.14
let small: num = 1e-3
An int can go where a num is expected, but not the other way around:
let x: num = 1 # fine, an int is a number
# error: `y` expects `int`, got `num`
let y: int = 1.5
Text
Strings use double or single quotes, and R's escape sequences such as
\n and \".
let greeting: char = "hello"
let name: char = 'world'
Booleans
Booleans are written true and false, in lowercase.
- Vapour
- R
let ready: bool = true
ready = TRUE
Missing and special values
R's missing and special values work as in R:
NA, NA_integer_, NA_real_, NA_character_, NA_complex_,
NaN, and Inf.
let unknown: int = NA
let limit: num = Inf
NA is accepted by any type. Vapour does not track NAs, but it does
track NULL, see types.
Comments
Comments start with #. Roxygen2 comments (#') are copied to the R
output unchanged, so you can document functions and
packages as usual.
#' Double a number
#' @param x A number
#' @export
func twice(x: num): num {
# multiply by two
stopifnot(!missing(x))
return x * 2
}
Helpful checks
Because every variable is declared, Vapour can tell you when something looks off:
# warning: `y` not found
print(y)
func f(): int {
let a: int = 1
# info: `b` is never used
let b: int = 2
return a
}
Using a variable that was never declared is only a warning, not an error, because it might be defined in R at run time.
Recap
- Declare with
let name: type = value, orconstfor values that never change. - Assign with
=, use+=and-=to add and subtract. <-assigns to a variable in a parent scope, like<<-in R.- A name can only be declared once per scope.
- Booleans are
trueandfalse, whole numbers areint, decimals arenum.
The precise rules are in the spec: literals, variables, and assignment.