Learn R Programming

oops (version 0.2.0)

change_formals: 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

Run this code
# NOT RUN {
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")

# }

Run the code above in your browser using DataLab