Skip to main content
Version: 0.0.5

Functions & Methods

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.

The way functions and methods are declared is taken from Go.

info

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

Functions​

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

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)
}

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.

type car struct {
int,
name: char
}

func (c: char) print(name: char): car {
c$name = 1
}

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
}