r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
405 stars 56 forks source link

Check Class Name Internally? #135

Closed cdeterman closed 6 years ago

cdeterman commented 6 years ago

Is there a way to have an R6 class return the name of it's class. For example:

myClass <- R6Class(
    "myClass",
    public = list(
        getClass = function(){
            #print(self$classname)
        }
     )
)

I would expect it to return "myClass"

I could simply add another field internally that simply populates self$classname as the class but I didn't know if there was an internal method already implemented like you would see with a python class like this self.__class__.__name__.

wch commented 6 years ago

You could do something like this:

myClass <- R6Class(
  "myClass",
  public = list(
    getClass = function(){
      class(self)[1]
    }
  )
)

m <- myClass$new()

m$getClass()
#> [1] "myClass"