Features
Type checking
Vapour checks for types (where it can), ensuring that your code is consistent
and that you do not get caught by unexpected NULLs
(see null), etc.
let x: int = 1
# will fail, x is an int
x = "hello"
let y: int | char = 1
# this will work
y = "hello"
y = 1
Variable, type, and function declaration
With variables declaration via let and const we can check
that variables are only declared once.
let x: int = 1
# will fail, x is already declared
let x: char = "a"
func foo(): null {
print("hello")
}
# will fail, foo is already defined
func foo(): null {
print("world")
}
type userid: int
# will fail, type userid is already defined
type userid: num
let id: userid = 1.1
# will fail, cannot use access operator on num
id$inaccessible = 1
type user: struct {
int,
name: char
}
let p: user = user(1, name = "John")
# will fail, no x attribute on user
print(p$x)
# will fail, name is of wrong type
p$name = 2
Functions cannot be named after a function from base R either.
# will fail, paste is a base R function
func paste(x: char): char {
return x
}
Constants
Vapour comes with constants so you can avoid squashing them on accident.
const x: int = 1
# will fail, it's a constant
x = 2
Variables existence
Vapour will check that you only reference variables that do exist.
# will warn that x does not exist
print(x)
Packages & functions
Vapour will warn you if you are using a package that is not installed or if you are using a function that is not exported by the package you call it from.
# will warn that the package is not installed
nonExistentPackage::foo()
# will warn that the function is not exported by that package
dplyr::some_function()
Variables & types used
Vapour will check that you use the variables you declare within functions.
func foo(): int {
let x: int = 1
# will warn that y is never used
let y: int = 1
return x + 2
}
Parent assignment
When assigning to a parent variable with <- (<<- in R),
Vapour checks that the variable exists.
let count: int = 0
func increment(): null {
count <- count + 1
}
func reset(): null {
# will fail, total does not exist
total <- 0
}
Iteration
Vapour checks that what you loop over can be iterated.
type user: struct {
char,
name: char
}
let p: user = user("a", name = "John")
# will fail, a struct cannot be iterated
for(let x: char in p) {
print(x)
}
Function calls
Vapour checks the arguments passed to functions you declare, see functions.
Missing check
Vapour will warn you if variables used within functions might be missing.
# will warn that x can be missing
func add(x: int): int {
return x + 1
}
# no warning
func add_safe(x: int): int {
stopifnot(!missing(x))
return x + 1
}
Decorators
Generic
Vapour lets you define a generic with a decorator.
- Vapour
- R
@generic
func (x: any) my_method(...: any): any
my_method <- function(x, ...) {
UseMethod("my_method")
}
Default
Vapour lets you define a default method with a decorator.
- Vapour
- R
@default
func (x: any) my_method(...: any): any {
stop("Not implemented")
}
my_method.default <- function(x, ...) {
stop("Not implemented")
}
Matrix
You can customise how matrices are created with @matrix,
this is making the type very strict.
It accepts nrow, ncol, and byrow.
- Vapour
- R
@matrix(nrow = 2, ncol = 2)
type mat: matrix { int }
mat(1..4)
structure(
matrix(
1:4,
nrow = 2,
ncol = 2
),
class = c("mat", "matrix")
)
Factor
You can customise how factors are created with @factor,
this is making the type very strict.
It accepts levels, labels, exclude, ordered, and nmax.
Arguments that are vectors or calls, e.g.: levels = ("a", "b"),
currently fail to parse.
- Vapour
- R
@factor(ordered = true)
type fct: factor { char }
fct(("a", "b", "a"))
structure(
factor(
c("a", "b", "a"),
ordered = TRUE
),
class = c("fct", "factor")
)
Environment
You can customise how environments are created with @environment,
it accepts hash, parent, and size (see ?new.env).
Arguments that are calls, e.g.: parent = emptyenv(), currently fail to parse.
- Vapour
- R
@environment(hash = true, size = 10)
type counter: environment {
count: int
}
counter(count = 0)
structure(
list2env(
list(count = 0),
envir = new.env(hash = TRUE, size = 10)
),
class = c("counter", "environment")
)