type: Post-process a function defintion to apply type constraints

View source: R/type.R

typeR Documentation

Post-process a function defintion to apply type constraints

Description

This function interprets the syntactic typing sugar and produces a standard R function, stripping type annotations from the function header and embedding them as a call to type_check as the first expression in the function body.

Usage

type(fn, ...)

Arguments

fn

A function to post-process

Value

fn with type annotations stripped from the function header and passed instead to a call to type_check.

Examples

fn <- type(function(a = 1 :numeric) {
  print(a + 2)
})

fn
#> function (a = 1) {
#>   type_constrain(type_check(character(0), a = numeric))
#>   print(a + 2)
#> }

fn(3)
#> [1] 5

fn("test")
#> Error in throw_type_check_error(signature, name) :
#>   Type of parameter 'a' does not match signature `numeric`


dgkf/typewriter documentation built on March 17, 2022, 5:16 p.m.