validate_dist_structure: Validate that an object satisfies the dist_structure protocol

View source: R/dist_structure-class.R

validate_dist_structureR Documentation

Validate that an object satisfies the dist_structure protocol

Description

Checks that x declares "dist_structure" in its class chain and provides the three required generics: ncomponents(), component(), and at least one of phi() or min_paths().

Usage

validate_dist_structure(x)

Arguments

x

An object claiming to satisfy the dist_structure protocol.

Details

Useful in subclass constructors to fail fast with a clear error message rather than at first method dispatch (where the user sees opaque "no applicable method" errors). A typical pattern:

my_subclass <- function(...) {
  obj <- structure(list(...), class = c("my_subclass", "dist_structure",
                                         "univariate_dist", "continuous_dist", "dist"))
  validate_dist_structure(obj)
  obj
}

Value

TRUE (invisibly) if all checks pass. Stops with an informative error otherwise.

Examples

# Success: a valid dist_structure (any built-in topology shortcut works).
validate_dist_structure(series_dist(replicate(3,
  algebraic.dist::exponential(1), simplify = FALSE)))

# Failure: an object that declares dist_structure but provides no
# primitives. Wrapped in tryCatch for the example's success status.
bad <- structure(list(),
                 class = c("not_a_real_class", "dist_structure",
                           "univariate_dist", "continuous_dist", "dist"))
tryCatch(validate_dist_structure(bad),
         error = function(e) conditionMessage(e))

dist.structure documentation built on May 13, 2026, 1:07 a.m.