Skip to main content
Version: Next

Types

Built-in Types

Vectors:

  • int - integer
  • num - numeric
  • char - character
  • bool - logical
  • date - date
  • posixct - Posixct
  • posixlt - Posixlt
  • any - any
  • null - NULL

Objects:

  • dataframe - data.frame
  • matrix - matrix
  • list - list
  • struct - structure
  • object - named list
  • factor - factor
  • func - function
  • environment - environment
info

Inferring types for base R and other packages is on the roadmap but likely will take quite some time to achieve.

Usage

Types are always preceded by a colon (:), e.g.:

let x: int = 1

Note that R has no scalar, therefore where you might expect let x: int to declare a scalar it actually declares a vector.

Any

warning

Use type any as little as possible as it defeats the purpose of a typed language.

You can use any if you cannot know the actual type, any means that any type is accepted.

let x: any = sum(1, 2, 3)

Multiple types

An object can be one of multiple types, separate the types with |.

let x: int | char

x = "hello"
x = 2

Custom types

You can declare your own custom types with the type keyword.

Vectors

You can define simple types such as below, these can seem underwhelming at first but quickly become interesting.

type userId: int

let john: userId = 1

Objects

You can define complex types such as dataframe, list, object, and struct.

List

In Vapour a list is like a list in R but cannot contained named objects, see the object type for that effect.

type lst: list { int | char }

let counts: lst = lst(1, "hello", 3, 4)

Dataframes

The dataframe type creates a data.frame, it expects named, typed, vectors.

type dataset: dataframe {
id: num,
name: char
}

let x = dataset(
id = 1..10,
name: "John"
)

Object

The object is a named list.

info

All attributes must be named in an object.

type thing: object {
id: num,
name: char
}

let x = thing(
id = 1..10,
name: "John"
)

Structs

A struct creates an R structure, the name of the struct is used as class.

type userId: int

type person: struct {
userId,
name: char,
age: int
}

let john: person = person(
0,
name = "John",
age = 36
)

Matrices

The matrix type creates a matrix, it expects a single type.

type mat: matrix { num }

let x = mat(1..10)

Factor

The factor type creates a factor, it expects a single type.

type fct: factor { char }

let x = fct(("a", "a", "b"))

Implied lists

You can define lists of objects by preceding your type with [].

type person: object {
name: char,
age: in
}

type people: []person

let group: people = people(
person(name = "John", age = 36),
person(name = "Jane", age = 35)
)