publicEval: Evaluate Expression with Public and Private Environments

View source: R/zlib.R

publicEvalR Documentation

Evaluate Expression with Public and Private Environments

Description

publicEval creates an environment hierarchy consisting of public, self, and private environments. The expression expr is evaluated within these nested environments, allowing for controlled variable scope and encapsulation.

Usage

publicEval(expr, parentEnv = parent.frame(), name = NULL)

Arguments

expr

An expression to evaluate within the constructed environment hierarchy.

parentEnv

The parent environment for the new 'public' environment. Default is the parent frame.

name

Optional name attribute to set for the public environment.

Value

Returns an invisible reference to the public environment.

Environments

  • Public: Variables in this environment are externally accessible.

  • Self: Inherits from Public and also contains Private and Public as children.

  • Private: Variables are encapsulated and are not externally accessible.

Examples

publicEnv <- publicEval({
  private$hidden_var <- "I am hidden"
  public_var <- "I am public"
}, parentEnv = parent.frame(), name = "MyEnvironment")

print(exists("public_var", envir = publicEnv))  # Should return TRUE
print(exists("hidden_var", envir = publicEnv))  # Should return FALSE


zlib documentation built on Oct. 19, 2023, 1:13 a.m.