readme.md

Static type checking in R

R package typeChecker

Implements, as an experiment, a primitive static type checker following the type annotation syntax proposed in DSC 2017 Syntax Extensions to R and the types package.

Installation and Usage

# install.packages("remotes")
remotes::install_github("kcf-jackson/typeChecker")

Perform type checking on a file with type_check(filepath), or on the active file in RStudio using type_check_active().

Examples

Example 1

dog <- function(name = ? character) {
    list(name = name)
}

dog("Archie")   # correct usage
dog(123)        # type error
# In the expression:
# dog(123)
# The following type error is found:
# Type mismatch. Inferred: numeric; Annotated: character

Example 2

dog <- function(name = ? character) {
    list(name = name) ? dog
}
introduce <- function(x = ? dog) {
    sprintf("Woof! My name is %s!", x$name)
}

x <- dog("Napawleon")
introduce(x)             # correct usage
introduce("Pawgustus")   # type error
# In the expression:
# introduce("Pawgustus")
# The following type error is found:
# Type mismatch. Inferred: character; Annotated: dog

How the package works

The package type-checks R files by looking for contradiction between the annotated types and the inferred types. If no contradiction is found, then the file is type-checked. Otherwise, it will report the expression in which the error is found.

Notes

Integration with editors

Visual Studio Code (VS Code)

The integration with VS Code is kindly provided by @andycraig. The feature supports exact tracking of the line and column numbers at which the type error occurs. Here is a screenshot:

To enable this feature, install the R LSP Client extension in VS Code and the R package languageserver as follows:

remotes::install_github("andycraig/languageserver@types")

The type-checking will then trigger automatically on R files.



kcf-jackson/typeChecker documentation built on Feb. 19, 2021, 2:29 a.m.