diff --git a/NAMESPACE b/NAMESPACE index c0dfa06a..a2b79330 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -75,6 +75,7 @@ export(method_explain) export(methods_register) export(new_S3_class) export(new_class) +export(new_empty_object) export(new_external_generic) export(new_generic) export(new_object) diff --git a/R/class.R b/R/class.R index 29887e47..35479fe0 100644 --- a/R/class.R +++ b/R/class.R @@ -252,8 +252,22 @@ new_object <- function(.parent, ...) { stop(msg) } - # force .parent before ... # TODO: Some type checking on `.parent`? + object <- new_empty_object(class, ..., .parent = .parent) + + # Don't need to validate if parent class already validated, + # i.e. it's a non-abstract S7 class + parent_validated <- inherits(class@parent, "S7_object") && !class@parent@abstract + validate(object, recursive = !parent_validated) + + object +} + +#' @param class The S7 class of the object to create. +#' @rdname new_class +#' @export +new_empty_object <- function(class, ..., .parent = class@parent()) { + # force .parent before ... object <- .parent args <- list(...) @@ -276,11 +290,6 @@ new_object <- function(.parent, ...) { for (name in names(prop_setter_vals)) prop(object, name, check = FALSE) <- prop_setter_vals[[name]] - # Don't need to validate if parent class already validated, - # i.e. it's a non-abstract S7 class - parent_validated <- inherits(class@parent, "S7_object") && !class@parent@abstract - validate(object, recursive = !parent_validated) - object } diff --git a/man/new_class.Rd b/man/new_class.Rd index ab260912..55735f43 100644 --- a/man/new_class.Rd +++ b/man/new_class.Rd @@ -3,6 +3,7 @@ \name{new_class} \alias{new_class} \alias{new_object} +\alias{new_empty_object} \title{Define a new S7 class} \usage{ new_class( @@ -16,6 +17,8 @@ new_class( ) new_object(.parent, ...) + +new_empty_object(class, ..., .parent = class@parent()) } \arguments{ \item{name}{The name of the class, as a string. The result of calling @@ -72,6 +75,8 @@ suppress validation when needed.} \item{.parent, ...}{Parent object and named properties used to construct the object.} + +\item{class}{The S7 class of the object to create.} } \value{ A object constructor, a function that can be used to create objects