[Rd] strange behaviour of callNextMethod in S4 methods
Harold PETITHOMME
harold.petithomme at meteo.fr
Thu Sep 23 16:21:11 CEST 2010
Hello,
I experienced a strange behaviour of callNextMethod when used in either
initialize or any other S4 function method definition.
Help says callNextMethod calls the next inherited method for the current
function from where it is called with the same actual (non missing)
arguments. This is OK. The problem appears when some formal arguments
(in particular, S4 objects) of this function are first used in the
current method and then must be used in the "next method" too. It
happens that such an argument is said to be missing in the "next
method". My guess is that using the argument in the current method makes
it disappear the next method. Is it a bug or a normal behaviour?
For understanding, let's see this example (with calls to some RNetCDF
functions):
Define a generic:
setGeneric("ncputVar",def=function(nc,variable,object)
standardGeneric("ncputVar"))
Define a 1st method for class Source:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="Source"),
def=function(nc,variable,object)
{
att.put.nc(nc,variable,"nom","NC_CHAR",object at nom)
att.put.nc(nc,variable,"dom","NC_CHAR",object at dom)
att.put.nc(nc,variable,"loc","NC_CHAR",object at loc)
att.put.nc(nc,variable,"time","NC_CHAR",object at time)
}
)
Define an inherited method for class OPSource:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="OPSource"),
def=function(nc,variable,object)
{
att.put.nc(nc,variable,"srctype","NC_CHAR","OPSource")
att.put.nc(nc,variable,"oporig","NC_CHAR",object at oporig)
callNextMethod()
}
)
A call to ncputVar with an OPSource object causes an error saying object
is not present.
But if the method definition is changed like this:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="OPSource"),
def=function(nc,variable,object)
{
o = object
att.put.nc(nc,variable,"srctype","NC_CHAR","OPSource")
att.put.nc(nc,variable,"oporig","NC_CHAR",object at oporig)
callNextMethod(nc,variable,o)
}
)
there is no more an error.
Why does my "object" disappear?
Thanks to all.
Harold
--
*********************************************************
Harold PETITHOMME
Equipe Données et Outils de Prévision (DPREVI/COMPAS/DOP)
Météo France - Direction de la Production
42, avenue G. Coriolis.
31057 Toulouse Cedex
France
Tel : (33/0)5.61.07.82.85
Fax : (33/0)5.61.07.86.09
E-mail : harold.petithomme at meteo.fr
More information about the R-devel
mailing list