DEV Community

Cover image for Operator Overloading in R
Andrew (he/him)
Andrew (he/him)

Posted on

Operator Overloading in R

Photo by Adli Wahid on Unsplash

Although operator overloading is generally a double plus ungood idea, it's good to know what a language is capable of, in case you find yourself in need of something in the future.

I recently discovered operator overloading in R.

Most R programmers know how to define a function:

> myfun <- function(x, y) x + y > myfun(3, 4) [1] 12 
Enter fullscreen mode Exit fullscreen mode

But you can also define a function with special symbols in its name, as long as you always surround that function name with backticks:

> `g^h` <- function(x, y) x + y > `g^h`(3, 4) [1] 12 
Enter fullscreen mode Exit fullscreen mode

R also provides the ability to create infix operators (operators which appear between their two arguments, like +). All you need to do is the above, but surround the function name with percent signs, as well:

> `%ytho%` <- function(x, y) x + y > 3 %ytho% 4 [1] 7 
Enter fullscreen mode Exit fullscreen mode

As you can see above, when using infix operators, you can drop the backticks. This is precisely how the R pipe operator is defined:

> "%>%" <- function(x,f) do.call(f,list(x)) > sqrt(pi) [1] 1.772454 > pi %>% sqrt [1] 1.772454 
Enter fullscreen mode Exit fullscreen mode

At this point, you might think: "hey, maybe I can redefine | to be the pipe operator in R, like it is in most shells". You would be correct:

> "|" <- function(x,f) do.call(f,list(x)) > pi | sqrt [1] 1.772454 
Enter fullscreen mode Exit fullscreen mode

But why don't we need percent signs around this function name? It's because you're actually not defining a new function, you're just overloading an existing one. In this case, it's the vectorised OR operator.

You can also do silly things like:

> "+" <- function(x, y) x * y > 3 + 4 [1] 12 > 4 + 5 [1] 20 
Enter fullscreen mode Exit fullscreen mode

...though I strongly caution against that. (It's confusing and never necessary.)

Top comments (0)