创建一个包含相同类型插槽的S4类

时间:2019-08-06 14:31:22

标签: r oop

作为实践,我正在使用S4类在R中创建一个LinkedList实现。

我有以下课程:

setClass("node",

  slots = list(
    value = "numeric",
    next_node = "node"
  ),

  prototype = list(
    value = NA_real_,
    next_node = NA
  ) 
)

但是,我收到以下错误消息:

Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) : 
  in making the prototype for class “node” elements of the prototype failed to match the corresponding slot class: next_node (class "node" )
In addition: Warning message:
undefined slot classes in definition of "node": next_node(class "node")

1 个答案:

答案 0 :(得分:1)

您可以使用:

setClass("node",

  slots = list(
    value = "numeric",
    next_node = "nullOrNode"
  ),

  prototype = list(
    value = NA_real_,
    next_node = NULL
  ) 
)

setClassUnion("nullOrNode", c("NULL", "node"))