Site icon R-bloggers

Double dispatch in R: S4 vs vctrs

[This article was first published on A Hugo website, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Why do we may need double dispatch? In most cases, when writing R scripts or even creating R packages, it is enough to use standard functions or S3 methods. However, there is one important field that forces us to consider double dispatch question: arithmetic operators. Suppose we’d like to create a class, which fits the problem we’re currently working on. Let’s name such class beer. beer <- function(type){ structure(list(type = type),class = "beer") } opener <- function(){ structure(list(), class = "opener") } pilsner <- beer("pilnser") my_opener <- opener() Then, we create an operator which defines some non-standard behaviour.

To leave a comment for the author, please follow the link and comment on their blog: A Hugo website.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.