type_define: Define a new type

Description Usage Arguments Examples

View source: R/package.R

Description

Define a new type

Usage

1
2
3
4
5
type_define(check = function(x) TRUE, error = function(obj_name, obj_value,
  type) {     sprintf("`%s` is a `%s` not a `%s`.", obj_name, if
  (is.object(obj_value))          class(obj_value)[[1L]]     else
  typeof(obj_value), type) }, document = character(),
  machine_type = character(), ...)

Arguments

check

A checking function to apply if that type is specified (optional). The function is passed one argument, the value of the object.

error

An error function to apply if the check fails. (optional). The function is passed the object name and value in two arguments.

document

A character vector that contains the documentation annotation for the type, optional and currently unused.

machine_type

A character vector that contains the machine type annotation for the type, optional and currently unused.

...

Additional optional fields.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type.character <- type_define( check = is.character)
type.integer <- type_define( check = is.integer)
prefix <- type_check(function(str = ? character, len = ? integer) {
  substring(str, 1, len)
})
## Not run: 
 prefix(10, 1), # `str` is a `double` not a `character`
 prefix("foo", NULL), # `len` is a `NULL` not a `integer`

## End(Not run)

jimhester/typeCheck documentation built on May 19, 2019, 10:34 a.m.