Types
Built-in Types
Vectors:
int- integernum- numericchar- characterbool- logicaldate- dateposixct- Posixctposixlt- Posixltcomplex- complexraw- rawany- anynull- NULL
Objects:
These are used to declare custom types, they cannot be used
directly as the type of a variable, e.g.: let x: list is invalid.
dataframe- data.framematrix- matrixlist- liststruct- structureobject- named listfactor- factorenvironment- environment
Functions:
func- function signature, see callbacks
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
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 |.
- Vapour
- R
let x: int | char
x = "hello"
x = 2
x = "hello"
x = 2
Null
Vapour checks for NULL: a variable or function can only
be (or return) NULL if its type says so.
# will fail, x cannot be NULL
let x: char = NULL
func lookup(x: int): int | null {
stopifnot(!missing(x))
if(x > 1) {
return x
}
return NULL
}
# will fail, lookup may return NULL
let y: int = lookup(2)
# this works
let z: int | null = lookup(2)
NA is a value, not a type, Vapour does not check for NAs.
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.
- Vapour
- R
type userId: int
let john: userId = 1
john = 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.
- Vapour
- R
type lst: list { int | char }
let counts: lst = lst(1, "hello", 3, 4)
counts = structure(
list(1, "hello", 3, 4),
class = c("lst", "list")
)
Dataframes
The dataframe type creates a data.frame, it expects named, typed,
vectors.
- Vapour
- R
type dataset: dataframe {
id: num,
name: char
}
let x: dataset = dataset(
id = 1..10,
name = "John"
)
x = structure(
data.frame(
id = 1:10,
name = "John"
),
class = c("dataset", "data.frame"),
names = c("id", "name")
)
Object
The object is a named list.
All attributes must be named in an object.
- Vapour
- R
type thing: object {
id: num,
name: char
}
let x: thing = thing(
id = 1..10,
name = "John"
)
x = structure(
list(
id = 1:10,
name = "John"
),
class = c("thing", "list")
)
Structs
A struct creates an R structure, the name of the struct is used as class.
- Vapour
- R
type userId: int
type user: struct {
userId,
name: char,
age: int
}
let john: user = user(
0,
name = "John",
age = 36
)
john = structure(
0,
name = "John",
age = 36,
class = "user"
)
Matrices
The matrix type creates a matrix, it expects a single type.
- Vapour
- R
type mat: matrix { num }
let x: mat = mat(1..10)
x = structure(
matrix(
1:10
),
class = c("mat", "matrix")
)
Factor
The factor type creates a factor, it expects a single type.
- Vapour
- R
type fct: factor { char }
let x: fct = fct(("a", "a", "b"))
x = structure(
factor(
c("a", "a", "b")
),
class = c("fct", "factor")
)
Environment
The environment type creates an environment, with typed attributes.
- Vapour
- R
type counter: environment {
count: int
}
let c: counter = counter(count = 0)
c$count = c$count + 1
c = structure(
list2env(
list(count = 0),
envir = new.env()
),
class = c("counter", "environment")
)
c$count = c$count + 1
See the @environment decorator to customise
how the environment is created.
Implied lists
You can define lists of objects by preceding your type with [].
Known issue: the outer users(...) call is currently not transpiled
to a structure, only the inner objects are.
- Vapour
- R
type user: object {
name: char,
age: int
}
type users: []user
let group: users = users(
user(name = "John", age = 36),
user(name = "Jane", age = 35)
)
group = structure(
list(
structure(list(name = "John", age = 36), class = c("user", "list")),
structure(list(name = "Jane", age = 35), class = c("user", "list"))
),
class = c("users", "list")
)