[R-pkg-devel] Function Overloading of S4 Methods

Georgi Boshnakov georg|@bo@hn@kov @end|ng |rom m@nche@ter@@c@uk
Sat May 18 14:39:36 CEST 2019


>Both are dispatched ONLY through the type of arguments.

Not quite. The dispatch may be by type but C++ identifies the arguments by position, while S4 applies R's dispatch rules
and respect the names, if present.  

Regarding your example from a previous email:

> ...You can write the previous function prototypes simply as
>
> int larger(int x, int y);
> char larger(char first, char second);

You can do this in C++ only because C++ doesn't care what you call the parameters. 
I don't know why you would want to do this (in C++ or R), it seems such a bad style even if possible.
In R you can define methods with different arguments, e.g. by declaring your function with a signature like:

larger <- function(x, y, first, second)

but then to use argument 'first' you need  to use it something like 'larger(first = whatever, ...)'. 


Georgi Boshnakov

-----Original Message-----
From: R-package-devel [mailto:r-package-devel-bounces using r-project.org] On Behalf Of Linus Chen
Sent: 18 May 2019 10:28
To: Dario Strbenac
Cc: r-package-devel using r-project.org
Subject: Re: [R-pkg-devel] Function Overloading of S4 Methods

Dear Dario,

I think differently, I think my examples show the resemblance between
the way of "dispatching" for S4 generic and that for C++ overloaded
function.
Both are dispatched ONLY through the type of arguments.

Again following your original example:
int larger(int x, int y);
char larger(char first, char second);
char larger(char x, char y){ # This will work!
if y>x y else x;
}
x = 'a' ; b='b';
larger(x,y);  # this will still call the second function.

The declaration "char largerChar(char first, char second);" does not
make any constraint on the name of argument...

Hope this helps.

Best,
Lei Chen

On Sat, May 18, 2019 at 10:00 AM Dario Strbenac
<dstr7320 using uni.sydney.edu.au> wrote:
>
> Good day,
>
> Your conclusion seems to be different to the details you discussed. Don't you intend to conclude that it's not possible with an S4 generic to define methods with different argument names which is different to a language such as C++?
>
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel

______________________________________________
R-package-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list