Skip to contents

new_function() declares a function whose parameters have types. The types are written in the function's own formals, with arg() and its shorthands, such as n = arg_integer(min = 1L, default = 10L), and each call checks and converts its arguments before the body runs, as a typed generic's call does (generic()). The function is registered under its name in the declaring package's namespace, as a type is.

Usage

new_function(
  name,
  fn,
  rules = list(),
  return_type = NULL,
  description = "",
  typed = "mixed"
)

# S3 method for class 'r8_function'
print(x, ...)

Arguments

name

Character: The function's name, an R8 name, under which it is registered.

fn

Function: The function, whose formals declare the parameters.

rules

A rule(), or a list of rules, over the typed parameters.

return_type

Optional: The type of the value that the function returns: a field type, such as type_double(), or an R8 type.

description

Character: What the function does.

typed

Character {"mixed", "strict"}: Whether every parameter has a type.

x

A typed function.

...

Not used.

Value

The function, of class r8_function, with the formals of fn.

Details

The body runs in the function's own frame, so substitute(), match.call(), sys.call(), parent.frame(), missing(), and on.exit() mean what they mean in a plain function. With return_type, each return() of the body, and its last value, pass the check of the result; a return() in a function defined in the body belongs to that function.

Rules

rules hold over the typed parameters, which a rule reads by name: rule(lo <= hi). They are checked once each argument satisfies its type. A rule that reads an optional parameter holds where its argument is NULL.

Typed and untyped parameters

With typed = "mixed", a parameter may have no type, and the function may take ...; a call checks the parameters that have types. With typed = "strict", every parameter has a type and there is no ..., which the declaration checks.

A body in Rust

A function of a package whose body is exactly rust() has its body in the package's Rust code: a function marked #[r8::function] of the same name, listed in functions of the package's r8::exports!. The declaration stays the function's: its parameters, types, defaults, descriptions, rules, return type, help page, and r8schema(). Each call checks and converts the arguments, evaluates every parameter, a default written in R included, and passes the values to the Rust function in the order of the parameters, ... as a list; the result is checked as an R body's is. An error that the Rust function returns is an r8_compiled_error. When the package loads, R8 compares the Rust function's signature with the declaration (r8rust_signature() writes it).

Outside R8

The function given is plain R: arg() gives its default, or R's error for a missing argument, and checks nothing.

Examples

op <- options(r8.namespace = "demo")
clip <- new_function(
  "clip",
  function(x = arg_vec(type_double()), lo = arg_double(), hi = arg_double()) {
    pmin(pmax(x, lo), hi)
  },
  rules = list(order = rule(lo <= hi)),
  return_type = vec(type_double())
)
clip(c(-1, 0.5, 3), lo = 0, hi = 1)
#> [1] 0.0 0.5 1.0
try(clip(c(-1, 0.5, 3), lo = 1, hi = 0))
#> Error : Invalid arguments of `clip()`: 1 problem
#> - Rule order: lo <= hi; found lo = 1, hi = 0. [rule_violation]
clip
#> <r8 function demo::clip>
#> clip(x, lo, hi)
#>   x: vector of double
#>   lo: double
#>   hi: double
#>   returns vector of double
#>   rule order: lo <= hi
options(op)