Skip to content

Tweak R6 usage#1815

Merged
hadley merged 7 commits into
mainfrom
tweak-r6-usage
Mar 18, 2026
Merged

Tweak R6 usage#1815
hadley merged 7 commits into
mainfrom
tweak-r6-usage

Conversation

@hadley
Copy link
Copy Markdown
Member

@hadley hadley commented Mar 17, 2026

Consistently refer to methods in the same way in every location.

Fixes #1258

* Use `obj$method()` not `{classname}$method()`
* Use `{classname}()` not `{classname}$new()`
* `rd_r6_method()` no longer needs alias

Fixes #1026
@hadley hadley requested a review from gaborcsardi March 17, 2026 14:41
Comment thread R/rd-r6-methods-self.R Outdated
Comment thread R/rd-r6-methods-self.R Outdated
if (method == "initialize") {
class
} else {
paste0("obj$", method)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know <class>$method is not technically correct, but it is more informative than the constant obj$ prefix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because then it's confusing that Foo$new() literally means to call Foo$new(), but Foo$something_else() does not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Is Foo_object$method() too long? How about adding a call that actually creates an object? Something like this?

Foo_object <- Foo$new(...)
Foo_object$method1()
...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those both feel a bit long to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just do $method()? Or is that too confusing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$method is not valid R code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

o <- Foo$new(...)
o$method1()
...
?

@hadley
Copy link
Copy Markdown
Member Author

hadley commented Mar 18, 2026

Here's a concrete example to consider:

Screenshot 2026-03-18 at 12 41 00 Screenshot 2026-03-18 at 12 41 25

It seems bad that we show the method in three different ways: Class$method(), method(), and obj$method(). I think I'm leaning towards Class$method() being the least worse option, but we should use it consistently everywhere. I think that means we can probably drop "Method" from the title, since it will be obvious from the context?

@hadley
Copy link
Copy Markdown
Member Author

hadley commented Mar 18, 2026

Overall, I think I like this approach, but it does feel a bit repetitive for simple methods:

Screenshot 2026-03-18 at 12 52 01

@hadley hadley merged commit 5753cd8 into main Mar 18, 2026
13 checks passed
@hadley hadley deleted the tweak-r6-usage branch March 18, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

R6 methods shouldn't have crossreferences to base functions of the same name

2 participants