readme.md

R package "declare"

Allows user to add optional type annotation to a function via comments. The package uses a simple mechanism, and it is designed such that the annotation may be "switched on and off" as one pleases. This is an experiment of programming in R with stronger type.

Coverage Status

Usage

examples.R

# Add one to a number
#$ add_one <- declare::type(add_one, x = "numeric")  # names on LHS and RHS must match
add_one <- function(x) { x + 1 }


# Column-bind two rows together
#$ cbind_rows <- declare::type(cbind_rows, r1 = "matrix", r2 = "matrix")
cbind_rows <- function(r1, r2) { cbind(r1, r2) }


# Multiply the first 3 rows of an object by a constant
#$ f <- declare::type(f, y = "numeric")  # partial specification
f <- function(x, y) { x[1:3, ] * y }

main.R

declare::source_with_type_annotation("examples.R")
add_one(10)          # Runs fine
add_one(nrow(1:10))  # Expect type error since nrow of a vector returns NULL.


X <- matrix(1:9, 3, 3)
Y <- matrix(1:3, ncol = 3)
cbind_rows(Y, Y)     # Runs fine
cbind_rows(X[1,], Y) # Expect type error since X[1,] is converted to vector implicitly.


x <- matrix(1:9, 3, 3)
x2 <- as.data.frame(x)
k <- 5
f(x, k)     # runs fine
f(x2, k)    # runs fine
f(x2, "A")  # type error

Notes



kcf-jackson/declare documentation built on May 29, 2019, 8:34 a.m.