change_formals: Change the Formal Arguments of a oClass Generator

View source: R/alter.R

change_formalsR Documentation

Change the Formal Arguments of a oClass Generator

Description

This accepts an oClass generator and updates its formal arguments based either on those passed in ... or the function passed to from_init. The results will be passed to the appropriate init function each time an instance is generated.

Usage

change_formals(x, ..., envir = parent.frame(), from_init = NULL)

Arguments

x

oClass generator function

...

named or unnamed objects used as the formal arguments of the generator function

envir

environment from which to evaluate arguments

from_init

function containing the formal arguments to use; typically an init function. ... and envir are ignored if not NULL.

Value

oClass generator function

Examples

clown <- oClass("clown")
clown

# 'init' requires a laugh
init.clown <- function(x, laugh, ...){
  x$laugh <- laugh
  add_fields(x, ...)
  return(x)
}

# change formals of clown
clown <- change_formals(clown, from_init = init.clown)

# alternatively,
clown <- change_formals(clown, laugh, ..dots)

# creation
happy_clown <- clown("HAHA")
sad_clown   <- clown("ha")


oops documentation built on March 18, 2022, 5:16 p.m.