Syntax
Vectors
In Vapour vectors are declared with parenthesis, without the need for c prefix.
- Vapour
- R
let x: int = (1, 2, 3)
x = c(1, 2, 3)
Range
The : is used to define types in Vapour, so we use .. for ranges.
- Vapour
- R
let x: int = 1..10
x = 1:10
For loop
The iterator needs to be declared with let.
- Vapour
- R
for(let i: int in 1..10) {
print(i)
}
for(i in 1:10) {
print(i)
}
Conditions
if, else if, and else work as they do in R.
- Vapour
- R
let x: int = 2
if(x == 1) {
print("one")
} else if(x == 2) {
print("two")
} else {
print("other")
}
x = 2
if(x == 1) {
print("one")
} else if(x == 2) {
print("two")
} else {
print("other")
}
While loop
- Vapour
- R
let i: int = 0
while(i < 3) {
i += 1
}
i = 0
while(i < 3) {
i = i + 1
}
Strings
Strings can use double or single quotes.
let x: char = "hello"
let y: char = 'world'
Comments
Comments start with #, roxygen2 comments (#') are kept as-is in the
transpiled code so you can document packages.
- Vapour
- R
#' Add one
#' @param x An integer
#' @export
func add_one(x: int): int {
# this is a comment
stopifnot(!missing(x))
return x + 1
}
#' Add one
#' @param x An integer
#' @export
add_one = function(x) {
# this is a comment
stopifnot(!missing(x))
return(x + 1)
}
Operators
Operators are the same as in R.
- Arithmetic:
+,-,*,/,^,%% - Comparison:
==,!=,<,>,<=,>= - Logical:
&&,||,! - Custom infix operators, e.g.:
%in%,%>%
let x: int = 10
let ok: bool = x > 1 && x <= 20
let z: num = x %% 3 + x ^ 2
Indexing
Use [, [[, and $ as in R.
let x: int = (10, 20, 30)
let first: int = x[1]
x[2] = 25
Pipe
The native pipe |> is supported.
- Vapour
- R
let x: num = (1, 2, 3) |>
sum()
x = c(1, 2, 3) |>
sum()
Namespaces
Call functions from other packages with ::, or ::: for unexported
functions, Vapour checks that the package is installed and that it
exports the function (see features).
let x: char = tools::toTitleCase("hello")
Return
In Vapour return is a keyword, not a function.
- Vapour
- R
func addOne(x: int): int {
return x + 1
}
addOne <- function(x) {
return(x + 1)
}
Defer
Vapour adds the defer keyword which translates to on.exit,
defer expects an anonymous function.
- Vapour
- R
func addOne(x: int): int {
defer (): null => {
print("second!")
}
print("first")
return x + 1
}
addOne <- function(x) {
on.exit((function(){
print("second!")
})())
print("first")
return(x + 1)
}
Declaration & assignment
In Vapour assignment is always done with the = sign,
<- is used for something else (see next section).
You also have to declare mutable variables with let, this
avoids shooting yourself in the foot overriding variables
that you should not.
- Vapour
- R
let x: char = "hello"
x = "hello"
Assign parent
In Vapour <- is used to assign to a parent variable,
where one would use <<- in R.
- Vapour
- R
let x: char = "hello"
if(x == "world") {
x <- "it's world!"
}
x = "hello"
if(x == "world") {
x <<- "it's world!"
}
Constants
We have constants!
This is only checked in Vapour, we do not lock the binding in the transpiled code.
- Vapour
- R
const x: char = "a constant!"
x = "a constant!"
Booleans
Booleans are denoted with true and false.
- Vapour
- R
const x: bool = true
x = TRUE
Assignments
Vapour supports addition (+=) and substitution assignments (-=).
- Vapour
- R
let x: int = 40
x += 2
x -= 1
x = 40
x = x + 2
x = x - 1
Missing values
R's missing and special values are available as they are in R:
NA, NA_integer_, NA_real_, NA_character_, NA_complex_, NaN, and Inf.
let x: int = NA_integer_
let y: num = Inf
NA is a value, not a type, it is accepted by any vector type.
Vapour does not check for NAs, see types for NULL.