SetMethod for two different object signatures in R

How do I do it with one setMethod, since the function section is the same in the next two lines? ("Triangle | Square"). Thank.

setMethod("sides", signature("Triangle"), function(object) 3)
setMethod("sides", signature("Square"), function(object) 3)

      

+3


source to share


1 answer


The usual approach

.sides_body = function(object) 3
setMethod("sides", "Triangle", .sides_body)
setMethod("sides", "Square", .sides_body)

      



if there is no class relationship and the definition is the same for all classes

setClass("Shape")
setClass("Triangle", contains="Shape")
setClass("Square", contains="Shape")
setClass("Circle", contains="Shape")
setMethod("sides", "Shape", function(boject) 3)
setMethod("sides", "Circle", function(object) Inf)

      

+4


source







All Articles