Skip to main content
Version: Next

Types

Every variable, argument, and return value in Vapour has a type. This chapter covers the built-in types and the rules Vapour uses to check them. You'll learn to define your own types in custom types.

Where types go​

A type always comes after a colon (:).

let x: int = 1

func half(x: num): num {
stopifnot(!missing(x))
return x / 2
}

Types only exist in Vapour: they are checked, then removed from the R output.

Built-in types​

TypeR equivalentExample value
intinteger1, 1e3
numnumeric (double)1.5, 1e-3
charcharacter"a"
boollogicaltrue
complexcomplex1i
rawrawas.raw(1)
dateDateSys.Date()
posixctPOSIXctSys.time()
posixltPOSIXltas.POSIXlt(Sys.time())
nullNULLNULL
anyanything

There are no scalars​

R has no scalars: a single number is a vector of length one. Vapour follows R, so int means "a vector of integers", whatever its length.

let one: int = 1
let many: int = (1, 2, 3)

Vectors​

Vectors are written with parentheses, there is no need for c().

let ids: int = (1, 2, 3)
let names: char = ("a", "b")
let nothing_yet: int = ()

All the elements of a vector must have the same type. R would quietly convert c(1, "a") into a character vector, Vapour reports it as an error instead.

# error: vectors of different types (int, char)
let mixed: int = (1, "a")

Note that 1 is an int and 2.5 a num, so (1, 2.5) mixes types too. Write (1.0, 2.5).

int and num​

An int can be used wherever a num is expected, because every integer is a number. The reverse is not allowed, a num could lose its decimals.

let a: num = 1         # fine

# error: `b` expects `int`, got `num`
let b: int = 1.5

Arithmetic follows the same logic: mixing int and num gives a num.

let x: int = 1
let y: num = x + 0.5 # fine

# error: `z` expects `int`, got `num`
let z: int = x + 0.5

One of several types​

Sometimes a value can legitimately be one of several types. Separate the types with | to declare a union.

let id: int | char = 42
id = "unknown"

A union is wider than its members: you can put an int in an int | char, but not an int | char in an int, because it might be a char.

let id: int | char = 42

# error: `n` expects `int`, got `int, char`
let n: int = id

null​

In R any function can return NULL, and code that doesn't expect it fails later, far away from the cause. Vapour tracks NULL: a value can only be NULL if its type includes null.

# error: `x` expects `char`, got `null`
let x: char = NULL

# fine
let y: char | null = NULL

This matters most for functions that sometimes have nothing to return. Say so in the return type, and Vapour makes every caller deal with it.

func find_user(id: int): char | null {
stopifnot(!missing(id))

if(id == 1) {
return "Ada"
}

return NULL
}

# error: `name` expects `char`, got `char, null`
let name: char = find_user(2)

# fine
let maybe: char | null = find_user(2)

NA is different: it's a value that any vector type can hold, and Vapour does not check for it.

any​

any accepts anything. Use it when you really can't know the type, e.g.: a Shiny input, or the result of readRDS().

let config: any = readRDS("config.rds")
warning

any switches off type checking for that value. Use it as little as you can.

any only goes one way: anything can go into an any, but an any can only go into another any.

let a: any = 1

# error: `b` expects `int`, got `any`
let b: int = a

Aliases​

The simplest custom type gives a new name to an existing type.

type user_id: int

let ada: user_id = 1

This looks underwhelming, but it makes code self-documenting and prevents mix-ups. A value can go into the alias, but an alias does not go back into its underlying type.

type user_id: int

let ada: user_id = 1

# error: `n` expects `int`, got `user_id`
let n: int = ada

A user_id can then never be passed by mistake to a function that expects some other int, like a count.

Values from R​

Vapour knows the types of the code you write in Vapour. It does not (yet) know the return types of R functions, so their results are accepted as any type.

# fine: sum() is an R function, its result is not checked
let total: int = sum(1, 2, 3)

It's up to you to declare the right type. When you are not sure, check with class() in R.

How Vapour decides​

A value of type S can go into a variable, argument, or return of type T when:

RuleExample
S and T are the samechar into char
S is int and T is num1 into num
T is a union containing Sint into int | char
T is anyanything into any
T is an alias of S1 into user_id
the value is NANA into anything

Everything else is an error.

Recap​

  • Types follow a colon: let x: int.
  • Every type is a vector type, there are no scalars.
  • Write vectors as (1, 2, 3), with elements of the same type.
  • Use | for unions, null for values that may be NULL, and any only as a last resort.
  • type name: int creates an alias.

The precise rules are in the spec: types and assignability.