isBeingCreated.Class.Rd
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.
# S3 method for class 'Class'
isBeingCreated(this, ...)
For more information see Class
.
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="")
})
#> NULL
print(Car("Volvo", 4))
#> [1] "Car: 0x600f79d15828"
print(Car("BMW", 4))
#> [1] "Car: 0x600f79c2d708"
print(Car("Tyrrell P34", 6))
#> [1] "Car: 0x600f79b7a6e0"
print(Car("T-Rex", 3))
#> [1] "Car: 0x600f79917408"