Skip to content

PRL-PRG/contractr

Repository files navigation

R-CMD-check codecov r-universe mutator

contractr

contractr adds run-time type contracts to R functions. You annotate a function with a type declaration; contractr injects assertions that check the arguments and the return value against that type every time the function is called, warning (or erroring, depending on the configured severity) whenever a value does not conform.

Types are written in the tastr type-declaration language.

Installation

contractr is published on the PRL-PRG r-universe:

install.packages("contractr", repos = "https://prl-prg.r-universe.dev")

To install from source you need a C++17 compiler. The tastr type library is bundled as a git submodule and built automatically at install time, so clone recursively:

git clone --recurse-submodules https://github.com/PRL-PRG/contractr
R CMD INSTALL contractr

Usage

library(contractr)

greet <- function(name) paste("hi", name)

# require a double argument and a character result
insert_contract(greet, "<double> => character")

greet(42)      # ok
greet("bob")   # contract violation: parameter 'name' expected double, actual character

You can also check or infer a value's type directly:

check_type(2L, "double")   # TRUE
infer_type(c(1.0, 2.0))    # "double[]"

About

An R 📦 to insert function argument and return type contracts

Topics

Resources

License

Stars

5 stars

Watchers

3 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors