[R] Generic Functions

Martin Morgan
Sun, 24 Feb 2008 16:58:48 -0000

See the 'useAsDefault' argument to setGeneric.

As an aside, if 'setType<-' is meant to be a 'setter' to
change the
value of a slot 'type', then I find the syntax a little redundant --
it's use

 > setType(x) <- "foo"

implies that it is already a 'setter' without 'set' at the
front. Why
not just

 > type(x) <- "foo"

(though perhaps 'type' is not such a good name, either)?

As a second aside, if you're writing code that you expect to be used
with fPortfolio, then having two functions with the same name but
different signatures or overall goals will confuse your user -- with
fPortfolio, setType<- works fine, but then for mysterious reasons (i.e.,
when your package is loaded, with a different definition of setType<-)
code that worked before no longer works! So I'd either use setType in a
way consistent with it's use in fPortfolio, or define a new generic for
your own purposes (setType<- is not a generic in my version of fPortfolio,

 > packageDescription('fPortfolio')$Version
[1] "260.72"

).

As a third aside, I think questions about S4 probably belong on R-devel,
as they seem to fall in the realm of 'questions likely to prompt
discussion unintelligible to non-programmers' (from the R-devel mailing
list description).

Martin

Dominik Locher wrote:
> Hi
>
> I have some problems in defining new generic functions and classes. Just
> have a look at the following example:
>
>
> require(fPortfolio)
>
> setClass("PROBECLASS",
>   representation(
>     type="character"
>   )
> )
>
> isGeneric("setType<-")
> #Returns
> TRUE
>
> #I would like to define a specific function for class PROBECLASS with other
> arguments than for the generic function "setType" of fPortfolio.
> setGeneric("setType<-", function(object, value)
> standardGeneric("setType<-"))
>
> #Returns
> Fehler in makeGeneric(name, fdef, fdeflt, group = group, valueClass >
valueClass,  :
>   the formal arguments of the generic function for "setType<-"
(object,
> value) differ from those of the non-generic to be used as the default
(spec,
> value)
>
> setReplaceMethod("setType", "PROBECLASS",
function(object, value){
>
>   object at type <- value
>   object
>
> })
>
> #Example
> obj = new("PROBECLASS")
> setType(obj) = "test"
> obj
>
>
> ######
> If I don't require fPortfolio it works fine. However, is it not
possible to
> create two generic functions with the same name but different arguments?
> setType for fPortfolio may be differ completely from setType of
> PROBECLASS...
> What's the best way to have functions which belongs to an object of a
> specific class? I had a look at the paper "S4 Classes in 15 pages,
more or
> less" (feb12, 2003), however, I could not found what I did wrong...
>
> Any help is highly appreciated.
>
> Thanks
> Dominik
>
> ______________________________________________
> 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.