Carl Witthoft
Carl Witthoft

Reputation: 21502

How to find all functions with methods for a given class

I'm basically looking for the opposite of methods(some_function) , which returns all class-methods that exist for that function. Is there some easy way to search for all functions which have an explicit method for a given object class?
For example, methods(my_func) returns a pile of myfunc.classname values. Is there a functions(my_class) which would return all functions with a func.my_class method?

Upvotes: 5

Views: 203

Answers (2)

Simon O'Hanlon
Simon O'Hanlon

Reputation: 59970

I think you want to supply an argument to class and nothing to generic.function in methods. Compare

methods(as.matrix) 
[1] as.matrix.data.frame              as.matrix.data.table*             as.matrix.default                
[4] as.matrix.dist*                   as.matrix.noquote                 as.matrix.POSIXlt                
[7] as.matrix.raster*                 as.matrix.SpatialGridDataFrame*   as.matrix.SpatialPixelsDataFrame*

With this, which returns methods for the generic class

methods(class="matrix")
 [1] anyDuplicated.matrix  as.data.frame.matrix  as.data.table.matrix* as.raster.matrix*     boxplot.matrix        corresp.matrix*      
 [7] determinant.matrix    duplicated.matrix     edit.matrix*          head.matrix           isSymmetric.matrix    lda.matrix*          
[13] qda.matrix*           relist.matrix*        subset.matrix         summary.matrix        tail.matrix           unique.matrix        

   Non-visible functions are asterisked

And this also seems to work for S4 classes as well, e.g.

methods(class="data.table")
 [1] $<-.data.table*           [.data.table*             [<-.data.table*           all.equal.data.table*     as.data.frame.data.table*
 [6] as.data.table.data.table* as.list.data.table*       as.matrix.data.table*     dim.data.table*           dimnames.data.table*     
[11] dimnames<-.data.table*    duplicated.data.table*    format.data.table*        head.data.table*          is.na.data.table*        
[16] merge.data.table*         na.omit.data.table*       names<-.data.table*       Ops.data.table*           print.data.table*        
[21] subset.data.table*        tail.data.table*          transform.data.table*     unique.data.table*        within.data.table* 

Upvotes: 8

Antoine
Antoine

Reputation: 558

I think you are describing the concept of introspection and reflection (well-known in Java).

A post about introspection and reflection in Java with links here : Java introspection and reflection

I don't know which technology or language you are using but maybe you will find the equivalent.

Hope this helps ! Bye !

Upvotes: 0

Related Questions