Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

isBeingCreated.Class

Checks if a class is currently being initiated initiated


Description

Checks if a class is currently being initiated initiated. When extending a class for the first time, which is typically done in a constructor, a static instance of the class is created by calling the constructor without parameters. This method provides a way to detect that second call inside the constructor.

Usage

## S3 method for class 'Class'
isBeingCreated(this, ...)

Arguments

...

Not used.

Value

Returns TRUE if a static instance exists, otherwise FALSE.

Author(s)

Henrik Bengtsson

See Also

For more information see Class.

Examples

setConstructorS3("Car", function(brand=NULL, nbrOfWheels=0) {
  if(!isBeingCreated(Car)) {
    if (is.null(brand))
      throw("A car must have a brand")
     
    if (nbrOfWheels <= 0)
      throw("A car must have one or more wheels: ", nbrOfWheels)
  }

  extend(Object(), "Car",
    .brand = brand,
    .nbrOfWheels = nbrOfWheels
  )
})

setMethodS3("as.character", "Car", function(this, ...) {
  cat(class(this)[1], ":", this$.brand, " with ",
                     this$.nbrOfWheels, " wheels.", sep="")
})

print(Car("Volvo", 4))
print(Car("BMW", 4))
print(Car("Tyrrell P34", 6))
print(Car("T-Rex", 3))

R.oo

R Object-Oriented Programming with or without References

v1.24.0
LGPL (>= 2.1)
Authors
Henrik Bengtsson [aut, cre, cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.