slotNames
适用于中四班级.例如:
setClass("myS4class", slots = c(x = "numeric", y = "numeric"))
getSlots("myS4class")
#> x y
#> "numeric" "numeric"
如果你看一下slotNames
的源代码,我们有:
function (x) {
if (is(x, "classRepresentation")) names(x@slots) else .slotNames(x)
}
这将始终调用函数.slotNames
,除非x
是可能从getClass("myS4class")
返回的"classRepresentation"
对象.
如果我们看一下.slotNames
的来源,我们会看到:
function (x)
{
classDef <- getClassDef(
if (!isS4(x) && is.character(x) && length(x) == 1L) x else class(x)
)
if (is.null(classDef))
character()
else names(classDef@slots)
}
这意味着如果我们传递类似于sin
的东西,我们将得到类"function"
的形式化的S4类定义
foo <- sin
foo_class_def <- getClassDef(class(foo))
这个正式的类定义如下所示:
foo_class_def
#> Class "function" [package "methods"]
#>
#> No Slots, prototype of class "function"
#>
#> Extends: "OptionalFunction", "PossibleMethod"
#>
#> Known Subclasses:
#> Class "classGeneratorFunction", from data part
#> Class "MethodDefinition", from data part
#> Class "genericFunction", from data part
#> Class "functionWithTrace", from data part
#> Class "activeBindingFunction", from data part
#> Class "refMethodDef", from data part
#> Class "derivedDefaultMethod", by class "MethodDefinition", distance 2
#> Class "MethodWithNext", by class "MethodDefinition", distance 2
#> Class "SealedMethodDefinition", by class "MethodDefinition", distance 2
#> Class "MethodDefinitionWithTrace", by class "MethodDefinition", distance 2
#> Class "standardGeneric", by class "genericFunction", distance 2
#> Class "nonstandardGenericFunction", by class "genericFunction", distance 2
#> Class "groupGenericFunction", by class "genericFunction", distance 2
#> Class "genericFunctionWithTrace", by class "genericFunction", distance 2
#> Class "defaultBindingFunction", by class "activeBindingFunction", distance 2
#> Class "internalDispatchMethod", by class "MethodDefinition", distance 3
#> Class "MethodWithNextWithTrace", by class "MethodDefinition", distance 3
#> Class "derivedDefaultMethodWithTrace", by class "MethodDefinition", distance 3
#> Class "nonstandardGroupGenericFunction", by class "genericFunction", distance 3
#> Class "standardGenericWithTrace", by class "genericFunction", distance 3
#> Class "groupGenericFunctionWithTrace", by class "genericFunction", distance 3
#> Class "refMethodDefWithTrace", by class "refMethodDef", distance 2
#> Class "externalRefMethod", by class "refMethodDef", distance 2
#> Class "refObjectGenerator", by class "classGeneratorFunction", distance 2
它已经命名了插槽,所以.slotNames
返回的插槽名称是NULL
names(foo_class_def@slots)
#> NULL
然而,如果foo
是一个长度为1的字符,就像您的第一个示例一样,.slotNames
将假定它是一个类名,并try 查找它的类定义.
foo <- "whatever"
foo_class_def <- getClassDef(foo)
foo_class_def
#> NULL
我们可以看到,这将导致.slotNames
返回character(0)
而不是NULL
.
为了保持一致性,我想知道在这种情况下,函数是否应该返回NULL
.