如何实现 S4 类 的长度函数?

How can I implement the length function for S4 classes?

我想在 R 中为 S4 class 实例实现 length() 函数。我会有一个 class:

setClass("A", slots = c(ll = "list"))

然后,我想创建 class A 的实例 inst 并调用 length(inst)

我之前的尝试都失败了,到目前为止,我只能找到一种方法来为 S3 classes 执行此操作。 (就像 Formula 包一样 here。)

假设您只想将 length() 方法委托给插槽“ll”。

setMethod("length", signature = "A", definition = function(x) length(x@ll))