Skip to main content
Version: Next

Functions

Functions are where types pay off the most. A typed function tells you what it needs and what it gives back, and Vapour checks every call.

Declaring a function​

Declare a function with func, its name, its arguments with their types, and its return type.

func add(x: int, y: int): int {
stopifnot(!missing(x), !missing(y))
return x + y
}

add(1, 2)

Read it as: add takes x, an int, and y, an int, and returns an int.

return is required​

In R a function returns its last expression, which isn't always obvious.

make <- function() {
x <- list()
x$name <- 2
}

make() # returns 2, not the list!

In Vapour, return is a keyword and it is required. Vapour checks that what you return matches the return type.

func make(): int {
# error: `make` is missing return
}

func label(x: int): char {
stopifnot(!missing(x))
# error: return expects `char`, got `int`
return x
}

Functions that return nothing​

Every R function returns something, there is no void. When a function is only called for what it does, not what it returns, give it the null return type. It doesn't need a return.

func log_message(msg: char): null {
stopifnot(!missing(msg))
cat(msg, "\n")
}

Arguments​

Missing arguments​

In R, forgetting an argument only fails when the function uses it, often with a confusing error. Vapour warns about arguments without a default that are used without checking they were passed.

# warning: `x` might be missing
func twice(x: int): int {
return x * 2
}

There are two ways to fix it: check with missing(), or give the argument a default value.

func twice(x: int): int {
stopifnot(!missing(x))
return x * 2
}

func greet(name: char = "World"): char {
return sprintf("Hello, %s", name)
}

greet() # "Hello, World"
greet("Ada") # "Hello, Ada"

Checked calls​

Vapour checks every call to a function you declared:

  • each argument has the right type,
  • named arguments exist,
  • there are not too many arguments.
func add(x: int, y: int = 2): int {
stopifnot(!missing(x))
return x + y
}

add(1)
add(y = 3, x = 1)

# error: argument #1 expects `int`, got `char`
add("a")

# error: could not find parameter `z`
add(1, z = 2)

# error: could not find parameter #3 (too many arguments?)
add(1, 2, 3)

Calls to R functions are not checked, only calls to functions written in Vapour.

Variadic arguments​

... takes any number of arguments. Like any argument, it needs a type.

func shout(...: char): null {
print(toupper(paste(...)))
}

shout("hello", "world")

Unique names​

Each argument must have a unique name.

# error: duplicated function parameter `x`
func add(x: int, x: int): int {
return x
}

Several return types​

A function can return one of several types, use |.

func parse_id(x: char): int | char {
stopifnot(!missing(x))

if(x == "") {
return "unknown"
}

return as.integer(x)
}

Include null when the function may return NULL, and callers will have to handle it, see types.

Anonymous functions​

Functions without a name, e.g.: for lapply, use an arrow (=>). The braces are required.

let doubled: any = lapply(1..3, (x: int): int => {
return x * 2
})

Functions as arguments​

When a function takes another function (a callback), you can declare the shape it must have with a function type: its argument types and its return type.

type combine: func(int, int) int

This is the only place where types are not preceded by a colon.

Use that type like any other, and Vapour checks every function passed to it.

type combine: func(int, int) int

func apply_pair(x: int, y: int, fn: combine): int {
stopifnot(!missing(x), !missing(y), !missing(fn))
return fn(x, y)
}

apply_pair(2, 3, (a: int, b: int): int => {
return a * b
})

# error: expected return `int`, got `char`
apply_pair(2, 3, (a: int, b: int): char => {
return "nope"
})

The callbacks example goes further.

Cleaning up with defer​

defer runs an anonymous function when the enclosing function exits, whether it returns normally or fails. Use it to close connections or remove temporary files. It transpiles to on.exit.

func work(): null {
defer (): null => {
print("cleaning up")
}

print("working")
}

work()
# [1] "working"
# [1] "cleaning up"

Scope​

Each function body is a new scope:

  • variables declared in a function are not visible outside of it,
  • a function can read variables from outside of it,
  • functions can be declared inside other functions.

To change a variable from outside the function, use <-, see variables.

Names​

A function cannot have the same name as a type, another function, or a base R function. Overwriting paste or c is a classic way to break R code in confusing ways.

type user: object {
name: char
}

# error: functions and types cannot share name (`user`)
func user(): null {
print("hello")
}

# error: function `paste` is already defined
func paste(x: char): char {
return x
}

Recap​

  • func name(arg: type): type { ... } declares a function.
  • return is required, unless the return type is null.
  • Check arguments with stopifnot(!missing(x)) or give them defaults.
  • Calls to your functions are checked: types, names, and number of arguments.
  • Anonymous functions: (x: int): int => { ... }.
  • Function types (type f: func(int) int) check callbacks.
  • defer runs code when the function exits.

The precise rules are in the spec: functions, anonymous functions, and calls.