Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document caller_arg() limitation in print() #1690

Closed
wants to merge 1 commit into from

Conversation

DanChaltiel
Copy link

Hi,

I stumbled upon the caller_arg() limitation when redefining a print method for hours without understanding, and thought it might deserve a bit of documentation.

As stated in https://stackoverflow.com/a/4963041/3888000:

This is a rather special case, as R substitutes foo by its value before calling print when you type the name at the command line.
[...]
There is no way on earth you'll extract the name of the object from anywhere. It's simply not there in the callstack.

Here is a rlang specific reprex:

my_obj=1
class(my_obj)="bar"
print.bar = function(x) rlang::caller_arg(x)
mean.bar = function(x) rlang::caller_arg(x)
my_obj
#> x
mean(my_obj)
#> [1] "my_obj"

#however, this works as intended
print(my_obj)
#> [1] "my_obj"

Created on 2024-02-21 with reprex v2.0.2

@lionel-
Copy link
Member

lionel- commented May 31, 2024

Thank you, unfortunately I think that this is a sufficiently specific edge case that this section would not be very helpful.

I'd welcome a PR adding a section that speaks more generally about the caveats of caller-arg/substitute in contexts where the argument might be inlined in the call stack. Print and auto-print could be used as an example to show the difference. This section would need to be pedagogical about the concepts involved while being sufficiently succinct that it does not take up too much space in the documentation.

@DanChaltiel
Copy link
Author

It is quite understandable.
Unfortunately, I'm afraid I'm not expert enough on the call stack to be pedagogical about it.

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.

2 participants