Types
Built-in Types
Vectors:
int
- integernum
- numericchar
- characterbool
- logicaldate
- dateposixct
- Posixctposixlt
- Posixltany
- anynull
- NULL
Objects:
dataframe
- data.framematrix
- matrixlist
- liststruct
- structureobject
- named listfactor
- factorfunc
- functionenvironment
- environment
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 = c("hello")
x = c(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.
- Vapour
- R
type userId: int
let john: userId = 1
john = c(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(
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(
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 person: struct {
userId,
name: char,
age: int
}
let john: person = person(
0,
name = "John",
age = 36
)
john = structure(
0,
name = "John",
age = 36,
class = "person"
)
Matrices
The matrix
type creates a matrix
, it expects a single type.
- Vapour
- R
type mat: matrix { num }
let x = 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(("a", "a", "b"))
x = structure(
factor(
c("a", "a", "b"),
),
class = c("fct", "factor")
)
Implied lists
You can define lists of objects by preceding your type with []
.
- Vapour
- R
type person: object {
name: char,
age: in
}
type people: []person
let group: people = people(
person(name = "John", age = 36),
person(name = "Jane", age = 35)
)
group = structure(
list(
structure(list(name="John"), class = c("person","list")),
structure(list(name="Jane"), class = c("person","list"))
),
class = c("people","list")
)