[R] Sealed for setGeneric

Martin Morgan mtmorgan at fhcrc.org
Thu Mar 13 15:11:24 CET 2008


Christophe Genolini <cgenolin at u-paris10.fr> writes:

> Hi the list
>
> When two setGeneric occurs on the same function, the second erage the 
> first and erase all the function previously define.

'erase' is only true if you attempt to define the generic in the same
name space (e.g., in the global environment, as occurs from the
command line, or in a single package).

> Is it possible to prevent that ? Is it possible to declare a setGeneric 
> that can not be erased later ?

Generics defined in different name spaces are not 'erased', but the
name space occuring earlier in the search path is 'masked' by generics
occuring later in the search path.

> getGeneric("show")
standardGeneric for "show" defined from package "methods"

function (object) 
standardGeneric("show")
<environment: 0x16b8b40>
Methods may be defined for arguments: object
Use  showMethods("show")  for currently available ones.
> show(1:10)
 [1]  1  2  3  4  5  6  7  8  9 10
> setGeneric("show", function(object) standardGeneric("show"))
[1] "show"
> setMethod("show", "numeric", function(object) "oops")
[1] "show"
> show(1:10)
[1] "oops"
> methods::show(1:10)
 [1]  1  2  3  4  5  6  7  8  9 10
> removeGeneric("show")
[1] TRUE
> show(1:10)
 [1]  1  2  3  4  5  6  7  8  9 10

> Something like the |sealed for setMethod...|

an unusual strategy would be to use lockBinding to make the symbol
associated with the generic unchangeable.

> setGeneric("show", function(object) standardGeneric("show"))
[1] "show"
> lockBinding("show", .GlobalEnv)
NULL
> setGeneric("show", function(object) standardGeneric("show"))
Error in assign(name, fdef, where) : 
  cannot change value of locked binding for 'show'
> unlockBinding("show", .GlobalEnv)
NULL
> setGeneric("show", function(object) standardGeneric("show"))
[1] "show"

Martin

> ||
> |Thanks|
> ||
> Christophe
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list