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.
# 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()
.
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
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
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.
10
,
character for 'a'
, and logical for TRUE
.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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.