tests/isBeingCreated.Class.R

message("TESTING: isBeingCreated()...")

library("R.oo")

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))

message("TESTING: isBeingCreated()...DONE")

Try the R.oo package in your browser

Any scripts or data that you put into this service are public.

R.oo documentation built on Nov. 2, 2024, 1:07 a.m.