Skip to main content
Version: 0.0.6

Functions & Methods

Introduction​

In Vapour functions are declared much differently than they are in R, methods even more so. This is both to make it more readable and easier to parse, R has some oddities with how methods are declared.

In R functions are declared like so:

foo <- function(x) {
x + 1
}

Whilst this works great for functions it becomes confusing when declaring methods, e.g.:

foo.data.frame <- function(x) {}

In the above, it is actually impossible to tell whether we are declaring:

  • A function called foo.data.frame
  • A method foo on data.frame
  • A method foo.data on frame

Either method declarations should have been done differently, or R should not have allowed . in function names, and other identifiers.

Hence, Vapour changes the way functions and methods are declared. The way functions and methods are declared is taken from Go. This is because the way methods are declared and dispatched in Go is very similar to R.

Return​

In Vapour return is a keyword rather than a function and is mandatory (except in functions that return null). Implicitly returning the last line is confusing.

Take for instance the function below, it's very hard for anyone to guess what it actually returns.

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

The above actually returns 2. Confusion only increases when the function makes use of branching.

foo <- function(x = FALSE){
x <- list()
if(!x) {
x$valid <- 1
} else {
x$valid <- "hello"
}
}

Therefore, we make the return keyword mandatory in Vapour.

Functions​

Functions are declared with the func keyword, we indicate the types of each argument as well as the type the function returns.

info

In Vapour return is a keyword and is mandatory, this is what the language uses to know what is returned by functions.

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

In R functions always return something there is no void, so use null instead.

func add(x: char): null {
print(x)
}

Arguments​

Default values​

Arguments can have default values, as in R.

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

Calling functions​

Vapour checks calls to the functions you declare: the type of each argument, that named arguments exist, and that you do not pass too many arguments.

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

# this works
add(1)
add(y = 3, x = 1)

# will fail, argument #1 expects int
add("a")

# will fail, there is no parameter z
add(1, z = 2)

Parameters must be unique.

# will fail, x is duplicated
func add(x: int, x: int): int {
return x
}

Variadic arguments​

Use ... for variadic arguments, it must be given a type like any other argument.

func log_all(...: any): null {
print(paste(...))
}

Multiple return types​

A function can return one of multiple types, separate the types with |.

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

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

return as.integer(x)
}

Anonymous Functions​

Anonymous functions are declared as shown below, inspired from Javascript, unlike Javascript Vapour expects curly braces ({}).

lapply(1..10, (x: int): int => {
return x + 1
})

Methods​

You can define methods on your custom types: place the type the method applies to in parentheses before the name of the function. This transpiles to an S3 method, name.class.

type car: struct {
int,
name: char
}

func (c: car) rename(name: char): car {
stopifnot(!missing(name))
c$name = name
return c
}

Declaring a generic with @generic is optional, without it only the method (rename.car above) is created, which is what you want when adding a method to an existing generic, e.g.: print or format.

Vapour lets you know if you define the same method twice on a type.

By default the class of an object is the name of its type, followed by the underlying R type, e.g.: c("car") for a struct, c("user", "list") for an object.

Class Decorator​

You can use the decorator @class to customise the class assigned to your object if you are not happy with the above.

@class(a, thing, on, wheels)
type vehicle: struct {
bool,
name: char
}

let train: vehicle = vehicle(
false,
name = "tchoo tchoo"
)

Generic Decorator​

You can use the decorator @generic to define a generic

type user: object {
id: int,
name: char
}

@generic
func (u: any) set_id(id: int): any

func (u: user) set_id(id: int): user {
u$id = id
return u
}

Names​

Functions cannot share a name with a type, nor with a function from base R.

type user: object {
name: char
}

# will fail, user is a type
func user(): null {
print("hello")
}

# will fail, paste is a base R function
func paste(x: char): char {
return x
}