Search code examples
rr-s4

Is it possible to not set types for s4 slots


What I want to achieve

In order to build a generic framework i would like to build an s4 class with a slot that could be of any type. Is it possible? And if so how?

What I have found so far

I have found this question that adress the multiple types problem. But I would like to make it possible for any types not just some predefined types.

Example:

setClass("foo",
         representation(
           anything = "..."
         )
)

# I would like to be able to perform all of these
new("foo", anything = 1)
new("foo", anything = "a")
new("foo", anything = data.frame())
...

Solution

  • Yes, you could do something like:

    setClass("hi", slots = c(slot1 = "ANY"))
    

    The use of ANY is actually documented in the help.