Re: [R] Sealed for setGeneric

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Thu, 13 Mar 2008 07:11:24 -0700

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

______________________________________________
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.
Received on Thu 13 Mar 2008 - 14:27:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 13 Mar 2008 - 23:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive