DEV Community

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

Posted on

5 3

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.)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs