r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
500 stars 137 forks source link

Can `as_function()` avoid materializing `call `? #1558

Closed DavisVaughan closed 1 year ago

DavisVaughan commented 1 year ago

Right now it is materialized at every call due to local_error_call()

https://github.com/r-lib/rlang/blob/33db700d556b0b85a1fe78e14a53f95ac9248004/R/fn.R#L389-L393

This makes it a bit slower than it needs to be if is_function(x) is already true, where it could just return immediately.

An easy fix would be to put that early return for if (is_function()) before the local_error_call() call, but I'm wondering if the local-error-call thing is really necessary?